简体   繁体   English

DataTemplate找不到类型,因为未知的命名空间Xamarin.Forms UWP

[英]DataTemplate can't find type because unknown namespace, Xamarin.Forms UWP

I'm working with Xamarin.Forms and I was trying to make a Custom Renderer for TabbedPage (UWP) and it kind of works... But when I try to create a new DataTemplate in C# referencing another Custom Renderer I get an error and I can't find any solution to that. 我正在使用Xamarin.Forms,正在尝试为TabbedPage(UWP)制作自定义渲染器,并且它的工作原理...但是当我尝试在C#中创建引用另一个自定义渲染器的新DataTemplate时,出现错误,并且我找不到任何解决方案。

Windows.UI.Xaml.DataTemplate GetStyledHeaderTemplate() {
    StringBuilder tpl = new StringBuilder();
    tpl.Append("<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"");
    tpl.Append(" xmlns:tint=\"clr-namespace:MyNamespace.UI;assembly=MyNamespace\"");
    tpl.Append(" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">");
    tpl.Append("<StackPanel Margin=\"0\" Padding=\"5, 0\" VerticalAlignment=\"Top\">");
    tpl.Append("<tint:ImageTinted TintColor=\"White\" Source=\"{Binding UWPIcon}\" Width=\"24\" Height=\"24\" Margin=\"0, 5, 0, 0\" />");
    tpl.Append("<TextBlock Padding=\"0\" Margin=\"0, 0, 0, 4\"");
    tpl.Append(" Text=\"{Binding Title}\" FontFamily=\"Segoe UI\" Foreground=\"White\" HorizontalAlignment=\"Center\" FontSize=\"13\" />");
    tpl.Append("</StackPanel>");
    tpl.Append("</DataTemplate>");
    return (Windows.UI.Xaml.DataTemplate)XamlReader.Load(tpl.ToString());
}

The code works fine when I don't call 我不打电话时代码正常

<tint:ImageTinted />

from the DataTemplate and ImageTinted works in other places. 来自DataTemplate和ImageTinted的作品在其他地方也可以。

Please, help! 请帮忙!

Error: 错误:

Windows.UI.Xaml.Markup.XamlParseException occurred
  HResult=0x802B000A
  Message=The text associated with this error code could not be found.

The type 'ImageTinted' was not found because 'clr-namespace:MyNamespace.UI;assembly=MyNamespace' is an unknown namespace. [Line: 1 Position: 339]
  Source=
  StackTrace:
   at Windows.UI.Xaml.Markup.XamlReader.Load(String xaml)
   at MyNamespace.UWP.StyledTabbedPageRenderer.GetStyledHeaderTemplate() in D:\...\MyNamespace.UWP\UI\Renderers\StyledTabbedPageRenderer.cs:line 91
   at MyNamespace.UWP.StyledTabbedPageRenderer.OnElementChanged(VisualElementChangedEventArgs e) in D:\...\MyNamespace.UWP\UI\Renderers\StyledTabbedPageRenderer.cs:line 35
   at Xamarin.Forms.Platform.UWP.TabbedPageRenderer.SetElement(VisualElement element)
   at Xamarin.Forms.Platform.UWP.Platform.CreateRenderer(VisualElement element)
   at Xamarin.Forms.Platform.UWP.VisualElementExtensions.GetOrCreateRenderer(VisualElement self)
   at Xamarin.Forms.Platform.UWP.Platform.<SetCurrent>d__51.MoveNext()
tpl.Append(" xmlns:tint=\"clr-namespace:MyNamespace.UI;assembly=MyNamespace\"");

将MyNamespace.UI更改为名称空间程序集的名称应为程序集的名称(在这种情况下,您的PCL项目的名称

In the UWP project containing your renderer, assuming MyNamespace.UI is the namespace of class ImageTinted and the assembly is referenced, replace this 在包含渲染器的UWP项目中,假设MyNamespace.UI是ImageTinted类的命名空间,并且引用了程序集,则替换为

tpl.Append(" xmlns:tint=\"clr-namespace:MyNamespace.UI;assembly=MyNamespace\"");

with the following: 具有以下内容:

tpl.Append(" xmlns:tint=\"using:MyNamespace.UI\"");

For example, if ImageTinted in the UWP project look like this: 例如,如果UWP项目中的ImageTinted看起来像这样:

<UserControl
x:Class="MyNamespace.UI.ImageTinted"
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">

<TextBlock Text="ImageTinted"/>

This should load the template in the XamlReader.Load statement. 这应该在XamlReader.Load语句中加载模板。

Debug by setting a breakpoint at the end brace of GetStyledHeaderTemplate() , ie after the return statement. 通过在GetStyledHeaderTemplate()大括号GetStyledHeaderTemplate()即在return语句之后GetStyledHeaderTemplate()设置断点来进行调试。 No exceptions from XamlReader.Load should be observed at this point. 此时,不应观察到XamlReader.Load异常。

If you still have errors then you should look into the ImageTinted class. 如果仍然有错误,则应查看ImageTinted类。

Here is an example UWP Renderer for TabbedPage: 这是TabbedPage的示例UWP渲染器:

[assembly: ExportRenderer(typeof(TabbedPage), typeof(MyTabbedPageRenderer))]
namespace MyRenderer.UWP
{
  public class MyTabbedPageRenderer : TabbedPageRenderer
  {
    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
            Control.HeaderTemplate = GetStyledHeaderTemplate();
        }
    }
  }

  private Windows.UI.Xaml.DataTemplate GetStyledHeaderTemplate()
  {
    string xaml = @"
<DataTemplate 
    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
    xmlns:tint=""using:MyNamespace.UI"">
<StackPanel Orientation=""Horizontal"">
    <tint:ImageTinted Source=""{Binding Icon}""/>
    <TextBlock Text=""{Binding Title}"" FontFamily=""Segoe UI"" 
FontSize=""13"" />
</StackPanel>
</DataTemplate>";

    return (Windows.UI.Xaml.DataTemplate)XamlReader.Load(xaml);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM