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.
Related
Running Visual Studio for Mac with Forms version 3.1.0.637273.
The documentation says to do this in the code-behind:
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
...
On<Xamarin.Forms.PlatformConfiguration.iOS>().SetUseSafeArea(true);
Or this in the Xaml:
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
ios:Page.UseSafeArea="true"
I've done both, and both give the same error:
Error CS1061: 'IPlatformElementConfiguration<iOS, Page>' does not contain a definition for 'SetUseSafeArea'
I get no error from just doing this:
On<Xamarin.Forms.PlatformConfiguration.iOS>();
Which does me no good, but at least it proves that I'm accessing the PlatformConfiguration.iOS stuff without an error.
Why isn't my code recognizing anything having to do with SafeArea stuff at all?
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.
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.
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.
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>