我在网上搜索时没有找到一种简单而干净的方式来实现我想要做的事情。
我在XAML中有一个控件public function save(Request $request, $id = null)
{
$obj = null;
if ($id === null) {
$obj = new CarCategory;
} else {
$obj = CarCategory::findOrFail($id);
}
return $this->saveHandler($request, $obj);
}
,它具有StackOverFlowObject
属性。
如果我正在执行以下操作:
Uint Questions
它不起作用,因为C#会将值读为<local:StackOverFlowObject Questions="23">
。
那么如果C#将值读作Int32,我该如何设置我的Uint32属性?
答案 0 :(得分:0)
那应该按原样运作,但是当你说它没有时我相信你。它甚至documented都不起作用。 (我是唯一一个在这里为懒惰负责的人,我会努力修复它。)
有两种方法可以实现这一点(实际上,3,第三种方法正在等待修复)
Questions
对您的[TypeConverter]
媒体资源进行归属,然后实施string
到UInt32
次转化。我打算建议使用像<x:UInt32>42</x:UInt32>
,but there's no primitive defined for UInt这样的Xaml2009语言基元,这样就不会有效了。
由于内置功能都不容易,我即将为您提供TypeConverter
选项的完整实现。
TypeConverter
public class StackOverFlowObject
{
...
[TypeConverter(typeof(UIntTypeConverter))]
public uint Questions {
get {} //This can be a normal property or an accessor for a bindable one
set {}
}
...
}
您可以从抽象Xamarin.Forms.TypeConverter
派生,因此已经为您编写了部分样板文件。此外,这允许在使用XamlC时使用一些快捷方式。
public class UIntTypeConverter : TypeConverter
{
public override object ConvertFromInvariantString(string value)
{
if (string.IsNullOrWhiteSpace(value))
return null;
return Uint32.Parse(value, CultureInfo.InvariantCulture);
}
}