silverlight resource incompatibility with VS designer? - silverlight

I have the following defined at the top of my XAML:
<controls:ChildWindow
x:Class="MyProject.SilverlightUI.Views.CharacterGenerator"
xmlns:my="clr-namespace:MyProject.SilverlightUI.ViewModels"
>
<controls:ChildWindow.Resources>
<my:AlignmentsViewModel x:Key="AlignmentsVM" ></my:AlignmentsViewModel>
<CollectionViewSource x:Key="AlignmentListViewSource" Source="{Binding Path=Alignments, Source={StaticResource AlignmentsVM}}"></CollectionViewSource>
</controls:ChildWindow.Resources>
Then later on I use the AlignmentListViewSource as an ItemSource that I bind to a ComboBox. Everything seems to work correctly except the Designer doesn't like the AlignmentsVM resource. It complains like this:
[Xml_CannotFindFileInXapPackage]
Arguments: ServiceReferences.ClientConfig
Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See [broken hyperlink]
at System.Xml.XmlXapResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlReaderSettings.CreateReader(String inputUri, XmlParserContext inputContext)
at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings, XmlParserContext inputContext)
at System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup()
I verified that the ServiceReferences.ClientConfig file IS part of the SilverlightUI project and is marked as "Content".
The AlignmentsViewModel calls a webservice in it's constructor, but I'm pretty sure this is a problem with the VS designer, not the class. I can compile and run the project and it runs exactly like I want it to from cassini, populating the combo box. it's just the VS designer that isn't cooperating.
Can anyone advise me as to how I can get the designer to recognize the fact that ServiceReferences.ClientConfig IS available?

The Visual Studio Designer actually calls the Control's constructor when showing it. If you access resources from the constructor or the UserControl_Loaded event handler, an exception occurs. I've worked around this (reasonable) limitations by providing save values while in designer.
Field declaration
static bool isInDesignMode;
Constructor logic
// Check for design mode (the Visual Studio Designer is unable to find the AppSettings.xml)
bool isInDesignMode = DesignerProperties.GetIsInDesignMode(this);
Method logic
string cultures = isInDesignMode ? "en,de" : ConfigurationManager.AppSettings["supportedCultures"];

Related

The type 'Window' does not support direct content

I have a WPF solution built with VS 2015 composed of several projects. Suddenly I started receiving a warning in design mode stating the following:
The type 'Window' does not support direct content.
I understand how some controls do not support direct content, but System.Windows.Window should. I get the same warning with UserControl, and as far as I know, any other control that typically supports direct content.
Everything compiles and runs fine, but having the blue underlines through all of my XAML is bothersome. Has anyone else come across this?
Below is a screenshot:
Make sure you reference System.Xaml. Clean and rebuild the project. Works on VS 2015 Update 1.
At least in a WPF IronPython project, adding the System.Xaml reference to the project solved the problem for me:
An important thing to note here is that adding seemingly any reference will make the problem go away temporarily -- until Visual Studio is restarted. System.Xaml, on the other hand, appears to keep the problem at bay. I even tried removing the reference, whereafter the problem returned upon restarting Visual Studio.
For me this error was happening because I added a WPF Window to a class library project.
For some reason (unknown by me), Visual Studio doesn't give us the option to select the WPF Window template from the "Add New Item..." dialog box if the project was not created as a WPF Application. Instead, it only offers the option to add a WPF User Control. Because of that, I selected the User Control template for the new item, and then edited the source code to make the XAML to become a Window object rather than a User Control.
<!-- The new item was created as an UserControl, but what I needed was a Window object. -->
<UserControl>
...
</UserControl>
<!-- Changed it to Window and made other necessary adjustments. -->
<Window>
...
</Window>
The problem was actually in the code-behind. Since it was created as an User Control, the window partial class was inheriting from UserControl, like the following:
public partial class MyWindow : UserControl
{
public MyWindow ()
{
InitializeComponent();
}
}
To fix it I just had to remove the inheritance, making my window class inherith from nothing, like this:
public partial class MyWindow
{
public MyWindow ()
{
InitializeComponent();
}
}
After removing the inheritance, Visual Studio didn't show the error "The type 'Window' does not support direct content." anymore.
on behalf of #mark Richman I edited the Itemtemplate to automatically Reference "System.Xaml".
Just in case some is interested:
can be found in: "[VS InstallDir]\Common7\IDE\ItemTemplates\VisualBasic\WPF\[InputLocale]\WPFWindow"
BR,
Daniel
Add System.Xaml and UIAutomationProvider references to your project, after that clear solution and then build again
in Visual studio 2019 :
I searched for ( System.Xaml.dll )
and I added it as a reference
its worked well
found it in this location:
" C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.8 "

How to set the default binding converter in Wpf?

I'm moving project from Silverlight to WPF and I've come across a problem.
I have a control with an INotifyPropertyChanged property GeoRect of type GeoRect. GeoRect has a variety of public properties that are set in its constructor each of type IGeoPosition.
I am setting a binding to one of these properties like so:
<TextBlock Text="{Binding GeoRect.TopRight, ElementName=x_SomeControl}"></TextBlock>
In Silverlight the default ToString method is called on IGeoPosition instance every time the GeoRect property changes. In Wpf I don't get any text at all.
I can correct this in Wpf by adding a ValueConverter to the TextBlock which simply calls the ToString method on the object, but this appears to be unnecessary fat. Can anyone help?
I suspect that there is another problem in your binding. Also in WPF, data binding calls the ToString() method to build the text of a Text-control.
Have you checked the output window of visual studio for a binding error? Or maybe the GeoRect-class does not support INotifyPropertyChanged for the TopRight property?
I guess that ElementName=x_SomeControl and GeoRect.TopRight are causing a probable "Source and Path" comination error. Are you sure your x_SomeControl has a property called 'GeoRect'? Also is x_SomeControl.GeoRect not null? And x_SomeControl.GeoRect.TopRight has a correct value?
As HCL pointed out, this will become apparent when you view your Output window where BindingExpression error must have appeared for this binding.
Please check.

Converting WPF application to UserControl for use in WinForms application via ElementHost

I'm trying to convert this WPF application to WPF UserControl so I could use it in WinForms application via ElementHost. I'm new to WPF and have never even touched it prior to this attempt so I might be going about it completely the wrong way.
I got the UserControl project to compile, however, my StaticResources get underlined in VS with message "The resource 'x' could not be resolved". I've tried moving the xamls with the needed x:Key elements up one level (the same level as the UserControl Dijagram.xaml), but it still cannot resolve them.
In the WinForms app, when trying to add the UserControl through designer by selecting hosted content in ElementHost tasks I get the following error:
An error occured trying to create an object of type
'DijagramLC.Dijagram'. Make sure the type has a default constructor.
(even though default constructor exists).
However, if I add it in codebehind, like this:
wpfUserControl = new Dijagram();
elementHost1.Child = wpfUserControl;
Controls.Add(elementHost1);
the code compiles but throws this runtime error: "System.Windows.Markup.XamlParseException: 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '20' and line position '4'. ---> System.Exception: Cannot find resource named 'MyToolbar'. Resource names are case sensitive."
I've tried with and without App.xaml and adding ResourceDictionary elements for problematic xamls, but basically, I have no idea what I'm doing :)
I've uploaded the code to my SkyDrive and would really appreciate it if someone could take a look and tell me what I'm doing wrong:
https://skydrive.live.com/redir.aspx?cid=21be1f8e850e85cc&resid=21BE1F8E850E85CC!353
I hate jumping in blindly to new techonoly like this, but I have had no choice this time, and need to know if my requirement is even achievable this way.
You are probably getting the error because you use resources from a resource dictionary which is not loaded. Loading a resource dictionary in the hosting application will probably solve the issue:
// When hosting a WPF usercontrol inside an element host,
// application resources are not loaded, so we need to load them manually.
var resources = new ResourceDictionary
{
Source = new Uri("/UNIT4.MKB.GUI.XAML.Dashboard.Resources;component/resources.xaml", UriKind.Relative)
};
// Check for null reference
if (Application.Current != null)
{
//Merge the loaded ResourceDictornairy with the dummy application Resources.
Application.Current.Resources.MergedDictionaries.Add(resources);
}
The problem is, you need THE default constructor like this:
public CreatedPollsUC()
{
InitializeComponent();
}
If you have any other code in the constructor, the error occures:
public CreatedPollsUC()
{
InitializeComponent();
// ... more code
}
So if you want to use further code in the constructor, you need to apply the control first to the element host. Then you can edit the constructor.

Error Cannot create an Instance of "ObjectName" in Designer when using <UserControl.Resources>

I'm tryihg to bind a combobox item source to a static resource. I'm oversimplfying my example so that it's easy to understand what i'm doing.
So I have created a class
public class A : ObservableCollection<string>
{
public A()
{
IKBDomainContext Context = new IKBDomainContext();
Context.Load(Context.GetIBOptionsQuery("2C6C1Q"), p =>
{
foreach (var item in SkinContext.IKBOptions)
{
this.Add(item);
}
}, null);
}
}
So the class has a constructor that populates itself using a domaincontext that gets data from a persisted database. I'm only doing reads on this list so dont have to worry about persisting back.
in xaml i add a reference to the namespace of this class then I add it as a usercontrol.resources to the page control.
<UserControl.Resources>
<This:A x:Key="A"/>
</UserControl.Resources>
and then i use it this staticresource to bind it to my combobox items source.in reality i have to use a datatemplate to display this object properly but i wont add that here.
<Combobox ItemsSource="{StaticResource A}"/>
Now when I'm in the designer I get the error:
Cannot Create an Instance of "A".
If i compile and run the code, it runs just fine. This seems to only affect the editing of the xaml page.
What am I doing wrong?
When running in the designer the full application runtime is not available. However the designer doesn't just magically know how to mock the UI of a UserControl. Its Xaml is parsed and the objects described there are instantiated.
Its up to you to code your classes to cope with existence in a designer. You can use the static proeprty DesignerProperties.IsInDesignTool to determine if your code is currently being used in a designer tool or not.
If in a designer you could deliver a set of test data rather than attempt to access a data service.
My problem is same as described above and i alse used DesignerProperties.IsInDesignTool
but i can't open usercontrol in visual studio for designing purpose

navigation framework in silverlight 3 skipping constructor when usercontrol is of a derived type

I am using the NavigationFramework in Silverlight 3, and am running into issues where the constructor of the UserControl in the xaml I am loading is not being called, and I believe this is because the UserControl in the xaml I am calling is actually derived from another user control.
I have stepped through the debugger with specific break points and the constructor is being ignored completey.
I have MyWindowBlue which is of type uctrlBaseMyWindow.
The constructor for uctrlBaseMyWindow is being called when the xaml is 'navigated to' but the constructor for MyWindowBlue is being ignored.
This is not the case if I add the user control via markup directly.
Anyone else have this issue?
The code I am using to navigate to the MyWindowBlue is
this.MyContentFrame.Navigate(new Uri("/Controls/uctrlMyWindowBlue.xaml", UriKind.Relative));
Has anyone run into this or could offer any help?
Thanks
Found the error in my code.
This was due to an error in my XAML. I had moved the user control to a different folder, and so the c# code behind had a type of the same name in one location in the namespace, but the XAML markup had the type in a different location (the original folder), and so the partial class with the constructor was not linked to the type I was actually instantiating.
I fixed the type reference to point to the proper location in the namespace and now the partial class code behind is linked back up.

Resources