Using ResourceDictionary from a DLL in Windows 8 - xaml

How can I use the ResourceDictionary resource from the same DLL?
Basically I am trying to create a UI library with all classes derived from Page class. I want to keep all user interface pages in the same DLL.
To see the problem, from VS2012, create a Windows 8 library project, then add the Item Detailed Page. Now, if you open the created page from the editor, you will get some errors like "The resource "LayoutRootStyle" could not be resolved".

This is just a Xaml Designer error, so that will not prevent your project from building or running .
The only thing needed is that all the ResourceDictonary need to be referenced by the main application App.xaml (for example by using <ResourceDictionary Source="/<myLibraryName>/Common/StandardStyles.xaml"/> or by creating calling an Init method in the Library which will dynamically add the Resource dictionary).
A quick workaround for the error in the Xaml Designer is to just copy an App.xaml/App.xaml.cs in your library (but at runtime the main application will still need to have a reference on the needed ResourceDictionary since the App.xaml of the library will not be used).
Another posibility is to just add a refrence on the ResourceDictionary on each page but I believe that will be much more costly since it will create an instance of the dictionary for each page.

Related

UWP Using Pages from different Project

Is it possible to have all pages in a different project from where the app.xaml/cs is in?
I tested this by creating a new "Class Library (Universal Windows)" or "Windows Runtime Component" project, then I created the new pages in those projects.
After I add this project as a reference to the main UWP app and call the page(s) in the "rootFrame.Navigate(typeof(MainShellView), e.Arguments)" I get the exception
system.accessviolationexception attempted to read or write protected memory
Is it possible to have the pages, controls in different projects than the UWP main project and use them as references?
You will need to keep an empty/dummy MainPage.xaml page in the main project as there is a hard-coded reference to it in the project system. With that in place you can load all your pages (incl. the initial start page) from other projects that you are referencing in the solution.
I will log a bug on the hard-coded dependency on the existence of "MainPage.xaml". Thanks for reporting!

XamlParseException when using UserControl from class library dll

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..

Setup a WinRt App with a reference to a Class Library which has a reference to a Class Library

So my project structure is as follows:
I have a Class Library that I am using made by XamlToolkit, since I had to tweak a certain handler I now have an implementation of my own.
This implementation is added as a reference to my BaseApp Class Library which contains Resource Dictionaries and some Code files for controls and such.
This BaseApp library builds into a Dll fine and using the "Generate library layout" option under Build options most issues got solved.
Now in the main WinRt app i have a reference to the BaseApp library and I try to merge the styles into the app as followed:
<Application.Resources>
<ResourceDictionary Source="ms-appx:///BaseApp/CoreFile.xaml" />
</Application.Resources>
This leaves me with the following error:
An error occurred while finding the resource dictionary "ms-appx:///BaseApp/CoreFile.xaml"
My first question would be, why is this not working? The source declaration should be no problem.
The second question would be, is it even possible to do something such as Class Library to Class Library to WinRt App?
Edit:
In a WinRT project you normally use a Source such as "/Folder/File.xaml". Like Kristian says you need to use the ms-appx:// syntax to reference another project (such as my class library).
The missing key is that in the Class library however you need to use the ms:appx// for all the source declarations to combine the files into 1 resource dictionary like we did in the CoreFile
It is possible to reference class library A in class library B, and then reference class library B in WinRT project. There should be no problem. But if you want to use code from A in WinRT project, then your WinRT project need to reference A too.
If CoreFile.xaml is in the root directory of the BaseApp class library then your code should work but instead you need to write like this in App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///BaseApp/CoreFile.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I never used the Generate library layout option and never got any problems, so I think this may be not needed, to prevent errors.

Any designer-friendly way to reference static resources inside class libraries without importing in every xaml file?

I am building a universal class library with Xaml Views that need to statically reference custom resources (StaticResource) defined inside the same class library.
How can i do that in a designer friendly way without importing the resource file inside every view?
Using VS2013 Update 3. It seems that the designer works only for views inside the specific universal app projects and when the resources are defined inside App.xaml without merging.

How efficient is XAML parsing in WinRT / Win8?

When creating UserControls, it looks like the XAML is being parsed every time the control is initialized.
For example, when I create a UserControl, there's auto-generated code to initialize the component that looks like this:
public void InitializeComponent()
{
if (_contentLoaded)
return;
_contentLoaded = true;
global::Windows.UI.Xaml.Application.LoadComponent(this, new global::System.Uri("ms-appx:///Views/MyView.xaml"), global::Windows.UI.Xaml.Controls.Primitives.ComponentResourceLocation.Application);
}
This existed in Silverlight as well.
Creating the control once or twice is not a big deal, but if I have a large visual tree in each UserControl with Visual-States and bindings, and I'm creating it many times per application lifecycle, it would make sense to build the visual controls using C# to boost performance.
So, my question is:
Does the parser/framework "remember" the XAML file and avoid re-parsing it again on subsequent calls? That is, does it create a binary representation of the XAML so it doesn't have to read text all over again?
It's my understanding that XAML gets compiled into a binary form as a resource within your application. The runtime does not have to parse the text of the .xaml file, just as it does not have to parse your .cs code files.
The performance of instantiating the classes as declared with XAML is supposed to be on par with creating it in code.
Windows 8.1 xaml finally added XAML binary format :)
XAML Binary Format: The final signed appx will no longer contain text based markup as it will get converted into Binary.