LDJ*_*LDJ 3 xaml xamarin xamarin.forms
我正在经历创建要在Xamarin Forms App的页面上使用的测试用户控件的过程。我想将主题作为字符串从测试页传递到控件中,然后根据一些不同的因素(例如用户设置和应用程序设置)将其转换为颜色。但是,一旦我在ContentView中声明转换器,我的页面就会停止呈现,而我得到的只是一个空白页面!
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyProject.UserControls.MyControl"
xmlns:helpers="clr-namespace:MyProject.Converters" >
<ContentView.Resources>
<helpers:StringToColourConverter x:Key="ColorConverter" />
</ContentView.Resources>
<ContentView.Content>
<StackLayout x:Name="ControlRoot">
<Label x:Name="PrimaryLabel" Text="{Binding PrimaryText}" />
<Label x:Name="SecondaryLabel" Text="{Binding SecondaryText}" />
</StackLayout>
</ContentView.Content>
</ContentView>
Run Code Online (Sandbox Code Playgroud)
如果我注释掉以下几行:
然后页面将按预期呈现主要和次要标签。但是,如果它在那里(甚至没有使用过!),则该页面为空白,并且我没有收到任何错误。我知道Converter可以正常使用了,因为VS2017为我自动完成XML声明中的命名空间,并自动完成名称StringToColourConverter的输入(如果我键入)<helpers:,但是我的转换器中的断点从未命中,因此代码甚至无法运行。
关于这里失败的任何想法?我必须在ContentView中以不同的方式声明掩护吗?
编辑1 对于转换器,我已将其剥离回去以确保它不是代码问题(我希望!):
public class StringToColourConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Color.Red;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
您是否按照以下方式注册了转换器?换句话说,您是否省略了样本的ResourceDictionary标签?据我所知,他们应该在那里。
<ContentView.Resources>
<ResourceDictionary>
<helpers:StringToColourConverter x:Key="ColorConverter" />
</ResourceDictionary>
</ContentView.Resources>
Run Code Online (Sandbox Code Playgroud)
另外,如果需要频繁注册这种转换器,则可以考虑在App.xaml中进行注册,这样就不必在需要的任何地方单独引用它。
<Application.Resources>
<ResourceDictionary>
<helpers:StringToColourConverter x:Key="ColorConverter" />
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
434 次 |
| 最近记录: |