Hi I'm trying to dynamically load xap files with MEF. I've got some bare bones code working with very basic Silverlight xaps. But loading xaps which are based on MVVM light I get a "Cannot find a Resource with the Name/Key Locator" which I guess is related to the host need something from MVVM Light. Can anyone with this? Cheers, Chris.
The error means that you are missing a resource called "Locator", usually being the ViewModelLocator. Look at your view XAML and you should find something like this:
<... DataContext="{Binding SomeViewModel, Source={StaticResource Locator}}" ...>
This line specifies the data context for an element. The data context is bound to the SomeViewModel property of a the static resource named Locator. Usually the locator is defined in the App.config by a statement similar to this:
<prefix:ViewModelLocatorClassName x:key="Locator"/>
The error most likely means that this line is missing.
Related
I have created a library which has a Popup UserControl similar to the one here.
When I create a fresh Universal Windows App and create the same UserControl inside an app and open the popup, it opens.
But if I create a Class Library and create the same UserControl inside it and try to use it (by opening the popup) inside an app, I get a XamlParseException.
It is as follows -
Windows.UI.Xaml.Markup.XamlParseException occurred
HResult=-2144665590
Message=XAML parsing failed.
Source=Windows
StackTrace:
at Windows.UI.Xaml.Application.LoadComponent(Object component, Uri resourceLocator, ComponentResourceLocation
componentResourceLocation)
at PopupTestLibrary.MyUserControl1.InitializeComponent()
I am not able to understand exactly why this is happening, since the code works fine when not called from an external class library.
Some Questions I found to be similar to mine, here on SO -
XamlParseException when consuming a Page from a library
Cannot instantiate UserControl from another assembly
All help is appreciated!
You need to Add a Resource Dictionary in your App and Add the Usercontrol Xaml content to it
as Xaml is Considered as a Content file not compiled into the code
I think that this post is just as yours.. :
https://social.msdn.microsoft.com/Forums/en-US/63f071be-a3c5-4f2d-ace2-73ca750e3252/rtm-usercontrol-class-library-and-assembly-name-with-
And, It's known issue:
Dot in the project's name cause XAMLParseException
I hope that this will help you in your issue..
In ReactiveUI, I run this code at a certain point:
const string template = "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:routing='using:ReactiveUI.Routing'>" +
"<routing:ViewModelViewHost ViewModel=\"{Binding}\" VerticalContentAlignment=\"Stretch\" HorizontalContentAlignment=\"Stretch\" IsTabStop=\"False\" />" +
"</DataTemplate>";
var theTemplate = XamlReader.Load(template);
On other platforms, this works great (the xmlns declaration is different of course), but on {WinRT / Metro / Windows Store}, this throws an Unspecified Error:
WinRT information: The type 'ViewModelViewHost' was not found. [Line: 1 Position: 253]
The Twist
However, if you include a dummy resource on the page:
<Page.Resources>
<DataTemplate x:Name="Foo">
<routing:ViewModelViewHost ViewModel="{Binding}" />
</DataTemplate>
</Page.Resources>
...then it works! What gives?
The "twist" makes me think this must be because the application does not have correct XAML metadata for the type being instantiated - rather than using reflection to resolve types in XAML files like WPF/Silverlight, WinRT uses code generation to resolve via the IXamlMetadataProvider interface (there's a decent description here; this sounds like what you're doing, see also the followup). Adding the reference forces this metadata code to be generated properly. If this is the case, you should be able to achieve the same effect by simply adding the type itself to the resources under some unused key, without the data template.
Have a look in your application's "obj" directory, Visual Studio generates a XamlTypeInfo.g.cs file to implement IXamlMetadataProvider. This should contain an entry for the type that is failing - in the case where you have added a dummy reference, there should be full details required to instantiate the type. Without this, I've found it's possible to have some reference to type type, but insufficient information - however this prevents the fallthrough behaviour (looking up the type in a dependent DLL which might have a custom metadata provider).
Other than adding a dummy reference to the library type in the final application itself, the only solution I found for this is to apply the Bindable attribute to the type. While this is supposed to relate to C++, I found this can be used in C# to force a type to always appear in the code generated for XAML type metadata.
For my windows 8 application i am trying to navigate between pages with out using code behind.
For example, i have one image in my UI without creating tapped event for that image i need to navigate to another page,
<Image Source="ms-appx:///Assets/Logo.png" Width="155" Height="110" Tapped="{ // Navigation method here }"/>
Is it possible to navigate between pages like this...? If possible, how can i get this to work??
XAML is just a declarative language without action part so code behind is an essential part of it.
All interactions work via events and event can be handled in a code behind only. So what you want is not possible with XAML(at least with WinRT XAML).
If you are asking if you can specify the code inside the .xaml file, then no, that is not possible.
If you are asking if you can avoid adding code to the .xaml.cs file, then yes, that is possible. You will still need to specify a method but it can even be done as a simple lambda. You will need to use the Command hooks rather than the Event Hooks, e.g.
<Button Command="{Binding GoConnectionCommand}" ... />
The code for this command is usually defined in the ViewModel as part of the MVVM pattern, and Josh Smith explains it far better than I will.
AlSki mentioned using a ViewModel. Although technically the ViewModel is not part of the "code behind" for the XAML file, it's still code and I believe you were asking for a no code solution.
ixSci is correct that there is no way to do this out of the box without code behind in WinRT XAML.
In full WPF it's possible to do this using a behavior called NavigateToScreenAction. You can read about it here. Unfortunately behaviors don't ship out of the box with WinRT, but they can be added back in by an open source project called WinRtBehaviors.
There is no NavigateToScreenAction behavior for WinRT, but one could be created. There is a good article on creating behaviors with the library here. It will obviously require code to create the behavior, but after it's created you could use it in XAML without any code.
Really, the short answer is it's not possible to navigate without code on WinRT.
Dev support, design support and more awesome goodness on the way: http://bit.ly/winappsupport
I am writing a win8 application and will be using the built-in resource management system: resw file and x:Uid tags in my XAML code.
So I create let's say a TextBox like that:
<TextBlock Style="{StaticResource HeaderTextStyle}" x:Uid="ResourceTest"/>
I create the corresponding resource file in my assembly with a ResourceTest.Text entry and it works fine: proper text is displayed at runtime.
Now, I would like to move all my resx files to another C# Library for maintainability. So I put the resources file in a brand new project and reference this new assembly from the main assembly.
But this causes the previous construct to fail (no text is displayed).
However, if I programmatically retrieve the resource value using the following code from inside the side assembly (called ResourcesLibrary), I get the string correctly:
static ResourceLoader resourceLoader = null;
public static string GetString(string resourceName)
{
if (resourceLoader == null)
resourceLoader = new ResourceLoader ("ResourcesLibrary/Resources");
return resourceLoader.GetString (resourceName);
}
How do I enable the x:Uid mechanism when dealing with out-of-assembly resources?
I tried a few things in the x:Uid such as ResourcesLibrary/Resources/ResourceTest but with no luck.
I had the same problem for a long time. But after testing a little bit, I solved it by writing the whole Path of the Resources in the XAML Code.
Something like this:
<TextBlock x:Uid="/ResourcesLibrary/Resources/ResourceTest" />
Unfortunately this answer came very late, but may it can help other persons.
As per my understanding you can't use x:Uid if the resources are maintained in a .resx file.
if you use .resw files you can access the strings whatever the assembly they are residing in.
they can be accessed as you mentioned in your question like this "ResourcesLibrary/Resources/ResourceTest"
I'd like to use the same data at design and runtime in my WP7 application. The relevant part of the XAML looks like this:
<phone:PhoneApplicationPage ...
d:DataContext="{d:DesignData Source=data.xaml}"
DataContext="{Binding Source=???}"
This way I can see the data at design time. Is there a way to use the same data at runtime?
Was not aware of the existence of d:DataContext. I dare to say:
<phone:PhoneApplicationPage ...
d:DataContext="{d:DesignData Source=data.xaml}"
DataContext="{Binding Source=data.xaml}"
--- Edit added -------------------
Well... checking here and there found that, as expected, "DesignData" is not the same as "Binding" (I was misreading this, sorry). "DesignData" has the ability to instance, at design time, an object based on a xml file and this has no effect at runtime. The Binding we are try to build will never work, as its purpose is to use already instanced objects.
When you run the default template for panorama applications it instances its own data (check the code for MainViewModel.cs)... and that's why it shows is different from that in the xml file. All you have to do is, manually, open the xml in the "LoadData" method and load the file data so you can display the same at runtime and design time.