Dynamic Data Display simple example gives couple of errors? - c#

<d3:Chart BottomTitle="Argument" LeftTitle="Function">
<d3:LineGraph x:Name="linegraph" Description="Simple linegraph" Stroke="Blue" StrokeThickness="3"/>
</d3:Chart>
This code is on the Home Page of the Dynamic Data Display. I downloaded .dll file and added the reference. Then I add those lines of code shown on the home page to my MainWindow.xaml
I get these errors;
'd3' is an undeclared prefix.
Chart is not supported in a Windows Presentation Foundation (WPF) project.
The namespace prefix "d3" is not defined.
I have no other code to show. This is the whole code because I was at the beginning.
Should I do something with this DynamicDataDisplay.XML file too? I didn't use it so far.

Try add this namespace to your Window/UserControl:
xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"

in Wpf you have to use "ChartPlotter" instead of "Chart". Chart is only supported in Silverlight.

Related

Mapsui Error: How to fix PresentationSource is null?

I have started to develop a WPF application with Mapsui. First I tried to get familiar with Mapsui in a seperat Visual Studio project. Now I want to include my code to my main project.
At the moment I get the following error message from which I can't figure out:
System.Exception: "PresentationSource is null"
In my test project for Mapsui I did not get this error.
I also tried to include my application code into the test project. But here I also get this error message.
<Grid Grid.Column="1" Grid.Row="0" Margin="10,10,10,10">
<xaml:MapControl Name="MapControl"></xaml:MapControl>
</Grid>
Maybe someone knows ideas I should take a closer look at or has a direct solution. Many thanks for your help!
The error may be caused by an error in the viewmodel constructor.
I received this error in a WPF view hosting a usercontrol containing the Mapsui mapcontrol in WPF MVVM application.
System.Exception
HResult=0x80131500
Message=PresentationSource is null
Source=Mapsui.UI.Wpf
StackTrace:
at Mapsui.UI.Wpf.MapControl.DetermineSkiaScale()
at Mapsui.UI.Wpf.MapControl.DetermineScale()
If I set RenderMode='Skia' or leave it blank I get the error.
Fixed error by setting RenderMode to Wpf in xaml in the usercontrol.
Setting this RenderMode to wpf also worked in code behind the usercontrol, setting it in the usercontrol's constructor.
<Wpf:MapControl RenderMode="Wpf" Name="myMapControl" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
or
this.myMapControl.RenderMode = Mapsui.UI.Wpf.RenderMode.Wpf;
The purpose beneath my user control was so I could create binding to Mapsui.MapControl.Map to a viewmodel.
Screen shot of Mapsui.mapcontrol hosted in a WPF usercontrol.

Create an instance of a C# class from inside XAML

I have a working application that has been written in C#, and I now want to extend that application to allow the user to switch between viewing the application, and viewing a built in web browser (inside the same application window).
I also have a separate working web browser, that has also been written in C#.
I have just added the functionality to the original application to include 'tabbed' displays, where the original application will be displayed on the first tab, and a built in web browser on the second tab.
The 'tabbed' displays for the application have been created using XAML markup in Visual Studio. I now want to add an instance of the Web browser that has also been written in C# to the second tab that I have created in the XAML markup.
It would be something like:
<TabControl>
<TabItem Header="Browser">
<StackPanel>
<!-- Call/ instantiate the browser here -->
</StackPanel>
</TabItem>
</TabControl>
But I have no idea how I call/ create an instance of the browser from within the XAML markup...
The browser has been created using C#:
namespace Agent
{
public partial class Browser : Form
{
public Browser()
{
...
}
}
}
Can anyone explain to me how a create an instance of Browser inside the ` of the XAML markup?
Edit
Ok, so I have edited my XAML markup as recommended in the answer that's been suggested- I now have:
<Window ...
xmlns:Agent="clr-namespace:Agent"
...>
<Grid>
...
<TabControl>
<TabItem Header="R">
<StackPanel>
...
</StackPanel>
</TabItem>
<TabItem Header="Browser">
<Agent:Browser x:Name="Browser" />
</TabItem>
</TabControl>
</Grid>
</Window>
I have also updated my Browser.cs class, so that it is now extending UserControl, rather than Form:
public partial class Browser : UserControl{
However, I am getting a compile error on the line:
<Agent:Browser x:Name="Browser" />
which says:
The name "Browser" does not exist in the namespace "clr-namespace:Agent".
But clearly Browser does exist in Agent, as shown by the code I've included here... In fact, when typing the line <Agent:Browser x:Name="Browser />, when I typed the :, Browser was one of the options that came up in the autocomplete menu...
What am I doing wrong here? Why doesn't the compiler think that Browser exists inside Agent?
The key to instantiating any object in XAML is to make sure the namespace is declared. You can provide any XML prefix and assign it to your CLR namespace (ref) and it will act like a using statement. For example:
<TabControl xmlns:agent="clr-namespace:Agent">
<TabItem Header="Browser">
<StackPanel>
<agent:Browser/>
</StackPanel>
</TabItem>
</TabControl>
NOTE: your object has to extend UIElement (or one of its children) for it to work in a XAML tree. If your control is a WinForms control you either need to find the equivalent XAML control or wrap it in a WindowsFormsHost (ref).
WPF vs. WinForms
The purpose of this section is to help recognize which platform code is by namespace, as well as some of the trade-offs. I've used both and can say from experience that they each have good points and... not so good points.
WinForms classes live in the System.Windows.Forms namespace, and are available by referencing the System.Windows.Forms.dll assembly.
WPF classes live in the System.Windows and System.Windows.Controls namespaces, and are available by referencing a set of DLLs
WinForms rendering is immediate. That means you are working against bitmaps and you are responsible for clearing and redrawing stuff yourself (usually you can just call Invalidate()). If you do heavy image bit manipulation, WinForms is easier to work with.
WPF rendering is declarative. That means more work is offloaded to your GPU and you just tell it how to draw stuff. You can also use GPU render shaders for special effects. WPF has a nicer look out of the box, but it has a reputation for making easy things difficult but impossible things possible.
WinForms is easier to learn, but has a dated look out of the box.
WPF is built around data binding, enabling the UI to update in response to property values automatically. It's also able to be completely restyled, although that is quite an undertaking.
If you are just getting started, I'd go ahead and bite the bullet to start the heavier learning curve for WPF. It will provide a basic understanding that transfers to other platforms like Windows Store apps, etc.
Firstly you need to place that tag inside of your UserControl opening tag like so:
<UserControl x:Class="View.testControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Agent="clr-namespace:Agent">
Then you can use it like this:
<TabControl>
<TabItem Header="R">
<StackPanel>
...
</StackPanel>
</TabItem>
<TabItem Header="Browser">
<Agent:Browser x:Name="Browser" />
</TabItem>
</TabControl>
EDIT
From what you told me in the comments you will need to create a Custom Control in your WPF project. To do that you need to:
Right Click your Project;
Select Add New Item;
From Installed (which is in the left column) select WPF;
From the list in the middle column select Custom Control;
Now you can create that control in your XAML with xmlns attribute.
Here is a great example from msdn on how to create custom controls

Adding class to use in XAML

I am learning about transparency and found some interesting answer for myself at
WPF: Detect Image click only on non-transparent portion
However I do not understand at all the part where it says in answer
after adding this class, just use it like a regular image:
utils:OpaqueClickableImage Name="image" Source="http://entropymine.com/jason/testbed/pngtrans/rgb8_t_bk.png" Stretch="None"
First of all I wrote the class and how do I add it in XAML now, so that it will be usable/visible? And if I may, since I'm a total beginner at this, to explain in step by step if its some thig complicated. Thank you for any kind of help/advice!
OpaqueClickableImage is a class that they have created which inherits from Image.
the "utils" part of the XAML snippet you posted refers to a custom namespace. You would need to add the name space so it would be visible. So if the namespace of your OpaqueClickableImage class was Program.Extensions you would need to add :
xmlns:utils="clr-namespace:Program.Extensions"
to your "Window" markup.
Then your OpaqueClickableImage would be usable just like any other image like this:
<utils:OpaqueClickableImage Source="image.jpg" width="100" height="100" />
Hope this makes sense...

How to localize a WP8 class library?

I'm trying to use resource files (.resx) in a class library. I'm having trouble using these resources in my library's XAML files because libraries do not come with an App.xaml file. So I can not do:
<Application.Resources>
<local:LocalizedStrings xmlns:local="clr-namespace:WPLocalization" x:Key="LocalizedStrings" />
</Application.Resources>
How do I go about localizing a self-contained WP8 library/assembly?
I found a way but it's rather a work around.
The solution is not to try to localize your controls from XAML, but instead from your behind code.
For example, you define a Button in XAML as follows:
<Button Name="MyButton" />
And then in your partial class behind you set the content of the button programatically as follows:
MyButton.Content = MyLocalizedStrings.Hello;
Of course, in this example you would have a resource file called "MyLocalizedStrings.resx" in your project with a string named "Hello" in it.
This approach solves the problem. The only down side is that you won't be able to see a preview of the localized XAML in the Visual Studio XAML window.

Problems with adding EnumMatchToBooleanConverter to my xaml file

I am trying to follow this radiobutton tutorial
I created a class called EnumMatchToBooleanConverter and it is in the top level of my wpf project. It says to place the inside a window.resources like this:
<Window.Resources>
<EnumMatchToBooleanConverter x:Key="enumConverter" />
</Window.Resources>
I am using it in a usercontrol so I have placed it inside a stackpanel instead:
<StackPanel.Resources>
<EnumMatchToBooleanConverter x:Key="enumConverter" />
</StackPanel.Resources>
I have Microsoft Visual Studio Ultimate 2012 and it gives me an error:
EnumMatchToBooleanConverter is not supported in a Windows Presentation Foundation (WPF) project.
Any ideas as to what I am doing wrong? Am I not allowed to place it inside a stackpanel.resources?
I just tried placing it inside a grid.resources
<Grid.Resources>
<EnumMatchToBooleanConverter x:Key="enumConverter" />
</Grid.Resources>
and it says
The type 'EnumMatchToBooleanConverter' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
Well it is in the same namespace as the rest of my project, so I'm unsure why it isn't finding it.
Change
<EnumMatchToBooleanConverter x:Key="enumConverter" />
for
<local:EnumMatchToBooleanConverter x:Key="enumConverter" />
All non-built-in classes you reference in XAML must be prefixed by their corresponding xmlns prefix.
HighCore's got it right. Just to add to this namespace discussion, I thought I'd point out another approach that can help make the code more readable or help you diagnose where certain Controls/Value Converters/etc are coming from (i.e., which assembly they are really coming from). This technique could allow your XAML to appear like Christian has it in his blog (without the xmlns prefix):
<EnumMatchToBooleanConverter x:Key="enumConverter" />
Essentially you perform some some namespace mappings to consolidate namespaces like this (only works if the files are in a different assembly/project). So in my example above you have mapped one of your namespaces to the default xmlns, so you would not need any prefix in the XAML.
I'm still trying to figure out how far to take this technique and Paul Stovell talks about taking it to the extreme, like I've shown above.
Even if you don't end up applying it to that degree, knowing about it might come in handy if you're looking at someone else's Xaml and they have applied a mapping like that. Knowing that would remind you to lookup the AssemblyInfo.cs file and check for the mapping and possibly help you track down where a Control/Value Converter/etc is actually located.

Categories

Resources