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

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.

Related

Maui using style.xaml from a class libary

I've created 2 projects in Maui. The first is a class library with a resource dictionary in it. The second is the actual app I want to use the resource dictionary so I tried referencing it in the App.xaml.
I have setup the GuiLibrary as a dependency of the app that needs to use it
All attempts have this at the top in the application section of the App.xaml
xmlns:gui="clr-namespace:AppGuiLibrary;assembly=AppGuiLibrary"
I've tried the following different combinations to no success.
<ResourceDictionary Source="AppGuiLibrary/Styles.xaml"/>
<ResourceDictionary Source="/AppGuiLibrary;component/Styles.xaml"/>
<ResourceDictionary Source="/gui;component/Styles.xaml"/>
The Styles.xaml is a Maui.Xaml content type.
The error I got for all of them is
Resource X not found
Does anyone know what I'm missing to get this to work?

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.

Using ResourceDictionary from a DLL in Windows 8

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.

MergedDictionaries outside the primary project

I have a solution with 2 projects. First is the primary application, the second has controls in it. In the Generic.xaml, in a Silverlight project, one would do
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyLibNamespace;component/Themes/SomeFolder/MyControl.xaml" />
</ResourceDictionary.MergedDictionaries>
This doesn't seem to work in a Windows Store application class library. ReSharper wants the filepath to be "/Themes/SomeFolder/MyControl.xaml" but I still get a XamlParseException: Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source'. [Line: 7 Position: 36]
There's no such thing as a "Xaml file within other class/assembly" in WinRT. In other words: "EmbeddedResource" or "Resource" simply doesn't exist in a WinRT component (and yes this is a pain). This is also why \Themes\Generic.xaml is copied out into the output directory).
See answer here on msdn
If you have a dll named "CustomControl" with a ResourceDictionary named "Styles.xaml", the App.xaml should contain
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///CustomControls/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Can reproduce the same behavior if the ResourceDictionary Source is set to a XAML that is a UserControl. However, if the XAML file referenced is a dedicated ResourceDictionary, there is no problem accessing internal styles with the ms-appx:/// scheme, following the ms-appx:///PROJECT_NAME/PATH_TO_XAML.xaml pattern.
Any chance the ResourceDictionary can be separated from the UserControl, if that is the case?

Error in Silverlight 4 + PRISM when a custom style is applied

The situation:
"Shell" project with App.xaml and a resource dictionary in Styles/Default.xaml with the interesting parts thus:
Default.xmal
<ResourceDictionary
<Style x:Key="StandardTextBox" TargetType="TextBox">
...
</Style>
</ResourceDictionary
App.xaml
<Application>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/Default.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
In a module project I've got a form with:
<TextBox Style="{StaticResource StandardTextBox}" />
At runtime I get the infamous "Error HRESULT E_FAIL has been returned from a call to a COM component." exception. The funny thing is during design time, in VS, the style is applied just fine in design mode. (and how VS.Net works the magic of knowing there's a resource in App.xaml in the Shell project - which is not referenced by the module project AT ALL - is baffling ... but I digress)
My overall goal is to have resources defined in files separate from App.xaml, in the Shell project, and have the styles applied intrinsically across the Module projects.
Update: Yeah, I was totally on the wrong war path here. The TextBox style that Blend generates references another style for the ValidationToolTip. Failing to include that will cause the issue described above. Unfortunately the error message was quite unhelpful and the squiggle underline in VS is easily missed when it's deep in the middle of the XAML definition and way off to the right. Live and learn.
The real issue was not including another referenced style. See this.