I have a CustomControl derived class in a dll (MyNamespace.UI). How can I use this control in my main application? I've tried adding a reference to the project and then using a custom XAML namespace to point to my namespace, but it can't find it:
<Page ...
xmlns:ui="using:MyNamespace.UI"
...>
<Canvas>
<ui:MyControl />
</Canvas>
</Page>
I get an error:
The name "MyControl" does not exist in the namespace "using:MyNamespace.UI".
Searching around, I managed to find the following quote:
The name of the assembly that defines the backing types for a XAML
namespace is not specified in the mapping. The logic for which
assemblies are available is controlled at the app-definition level and
is part of basic app deployment and security principles. Declare any
assembly that you want included as a code-definition source for XAML
as a dependent assembly in project settings.
http://msdn.microsoft.com/en-gb/library/windows/apps/jj150588.aspx
Can someone point me in the direction of the "dependent assembly" setting in the project settings?
Edit After making sure the root namespace project setting matched the namespace of my control (MyNamespace.UI) I could get the project to compile. But I can't get it to run because I get loads of crashes with the following information:
WinRT information: Cannot create instance of type 'MyNamespace.UI.MyControl' [Line: 44 Position: 37]
No information on why it couldn't make the control though... I stepped through the constructor of MyControl and the exception is thrown in InitializeComponent(). This is the XAML of my UserControl (MyControl):
<UserControl
x:Class="MyNamespace.UI.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
</Grid>
</UserControl>
Edit 2 I deleted the Grid control, built and ran, and it worked, so then I put the Grid control back, and it still worked. Very weird.
You need to make sure the MyNamespace.UI assembly has a RootNamespace property that matches MyNamespace.UI. This property can't be changed in the project settings pages, but you can do it via notepad or equivalent. My RootNamespace was set to UI, and when I changed it, it started working.
Related
I am trying to add TeachingTip to Xaml file in a usual way:
<TeachingTip/>
But I get an error "The type 'TeachingTip' was not found".
I tried to add programmatically in cpp file (C++/CX):
TeachingTip^ tip = ref new TeachingTip();
The error is: "Identifier 'TeachingTip' is undefined".
All other controls work well.
The TechingTip control is part of the Windows UI Library, not Windows. As such it resides in namespace Microsoft.UI.Xaml.Controls (as opposed to Windows.UI.Xaml.Controls). To reference that control in XAML, you'll have to use the correct namespace. Getting started with the Windows UI Library has detailed instructions.
The following XAML fragment should work:
<Page x:Class="..."
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
...
>
<muxc:TeachingTip/>
...
</Page>
Likewise, when referencing the control in code, you'll have to specify the correct namespace.
I am working on making a separate set of views for mobile devices, rather than using additional adaptive UI states for the phone. I am able to achieve this by adding a sub-folder in my Views folder called DeviceFamily-Mobile and adding a new View with the same name as the one I am overriding.
I have the following View that will work and display "MOBILE" on a mobile device/emulator.
<Page x:Class="MyApp.PayeesPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:behaviors="using:Template10.Behaviors"
xmlns:controls="using:Template10.Controls"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:models="using:MyApp.Models"
xmlns:viewModels="using:MyApp.ViewModels"
xmlns:views="using:MyApp.Views"
mc:Ignorable="d">
<RelativePanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock x:Name="MobileTextBlock"
Foreground="{ThemeResource ForegroundColorBrush}"
Text="MOBILE" />
</RelativePanel>
</Page>
However, if I try to set the DataContext to allow me to actual display something useful, like so:
<Page.DataContext>
<viewModels:PayeesPageViewModel x:Name="ViewModel" />
</Page.DataContext>
Then I get an error when navigating to the PayeesPage:
Unable to cast object of type 'Windows.UI.Xaml.Controls.TextBlock' to type 'MyApp.ViewModels.PayeesPageViewModel'.
This is the same way I set the DataContext on the original PayeesPage and it works fine. Is there a different way to set the DataContext on these alternate Views, or am I missing something?
It turns out that the mobile View needs to have the same x:Class as the original Page. ReSharper was complaining when I tried to call it MyApp.Views.PayeesPage because that already existed, so I changed it to MyApp.PayeesPage. However, when I switched it back so that both were using the same x:Class then everything started working as expected.
Unfortunately, I am getting a bunch of red squigglies from ReSharper, but things are working as they should be. Just in case anyone comes across this question in the future:
Views/PayeesPage.xaml:
<Page x:Class="MyApp.Views.PayeesPage"
...>
Views/DeviceFamily-Mobile/PayeesPage.xaml:
<Page x:Class="MyApp.Views.PayeesPage"
...>
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?
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.
Suppose I have a top-level ResourceDictionary element in a XAML file with no C# code-behind. How can I put this element in a specific namespace?
I'm not sure if you can -- but I've never seen this done.
What I have seen is putting ResourceDictionary XAML files in a directory structure in an assembly, and getting it via the Pack URI syntax.
Dr. WPF has an interesting post on getting ResourceDictionaries in code here.
It is much simpler than I had thought:
<ResourceDictionary x:Class="SomeNamespace.SomeClassName"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</ResourceDictionary>