在XAML中,我有以下一行:
In XAML I have the following line:
<Image x:Name="MainImage" Source="{x:Bind ViewModel.MainPic,Mode=OneWay,TargetNullValue={x:Null}}" Stretch="UniformToFill"/>在ViewModel中:
In ViewModel:
public string MainPic { get { if (Data == null) return default(string); else return Data.Photos.ElementAtOrDefault(0).url; } }应用编译正常,但在执行过程中(由于在几秒钟后填充了数据),该应用崩溃,但出现以下异常:
App compiles fine but during execution (since Data is populated after few seconds), the app crashes with the following exception:
System.ArgumentException:参数不正确.
System.ArgumentException: The parameter is incorrect.
调试器在以下位置中断
private void Update_ViewModel_MainPic(global::System.String obj, int phase) { if((phase & ((1 << 0) | NOT_PHASED | DATA_CHANGED)) != 0) { /*HERE>>*/ XamlBindingSetters.Set_Windows_UI_Xaml_Controls_Image_Source(this.obj23, (global::Windows.UI.Xaml.Media.ImageSource) global::Windows.UI.Xaml.Markup.XamlBindingHelper.ConvertValue(typeof(global::Windows.UI.Xaml.Media.ImageSource), obj), null); } }显然,这是因为MainPic返回null.
Apparently, this occurs since MainPic is returning null.
现在,此代码在WP8.1中可以正常工作.我尝试返回uri,这会导致编译时错误.我相信在Win 10中只能将字符串绑定到图像源(?),我只想在数据填充之前留白的空白区域,因此,我不希望将本地图像源作为后备.有人可以帮我移植到Win 10吗?
Now, this code works fine in WP8.1. I have tried returning uri which results in Compile time error. I believe only string can be binded to image source in Win 10 (?) I just want a blank white area until data is populated hence I don't wish to give a local image source as fallback. Can someone help me port this for Win 10?
更新:
感谢回答问题的用户,得出以下结论(对于UWP):
Thanks to the users who answered, following conclusion is drawn (for UWP):
- 如果要将图像源绑定到 string ,则不能为 null 或空的" .单字符"x" 或空格" 都可以.
- 如果绑定到 BitmapImage ,则返回 null 即可.
- 您可以使用@ Justin-xl提到的任何方法.为我,更改所有虚拟机以停止返回null很难.所以添加一个简单的将转换器转换为xaml也可以解决问题.
- If you're binding image source to a string, it cannot be null or empty "". A singe character "x" or a space " " would work.
- If you bind to a BitmapImage, returning null works.
- You can use any of the methods mentioned by @Justin-xl . For me, changing all vm's to stop returning null was hard. So adding a simple convertor to xaml also does the trick.
这是转换器代码:
public object Convert(object value, Type targetType, object parameter, string language) { if (string.IsNullOrEmpty(value as string)) { return null; } else return new BitmapImage(new Uri(value as string, UriKind.Absolute)); } 推荐答案如果使用 x:Bind ,则 Image 需要绑定到与 ImageSource 完全相同类型的属性(例如, BitmapImage ),而不是 string ,否则它将引发编译-time错误,这正是编译时绑定应该执行的操作.旧的绑定允许 strings ,因为它在运行时使用 Reflection 来为您解析类型.
If you use x:Bind, the Source of the Image needs to bind to a property of the exact same type ImageSource (e.g. BitmapImage) instead of string, otherwise it will throw a compile-time error, which is exactly a compile-time binding is supposed to do. The old binding allows strings 'cause it uses Reflection to resolve the type for you during run-time.
结果证明我的显式类型理论是错误的(感谢@igrali指出了这一点). Source 确实需要一个 string ,只要它不是 null 或''.因此,我们有两个解决方案.
Turns out my explicit type theory was wrong (thanks to @igrali for pointing it out). The Source does take a string as long it's is not null or ''. So it leaves us two options to fix this.
选项1
将 uri 保留为 string ,但是在 vm 为 null 或'',返回一些伪文本(即使返回字母 x 也可以!).
Keep your uri as a string, but do a check in your vm, once it's null or '', return some dummy text (even returning an a letter x would work!).
选项2
将 uri 从 string 更改为 BitmapImage .然后,您可以使用 TargetNullValue 和 FallbackValue 处理空值和无效绑定.
Change the uri from a string to a BitmapImage. Then you can use TargetNullValue and FallbackValue to handle nulls and invalid bindings.
... FallbackValue='Assets/SplashScreen.png' TargetNullValue='Assets/SplashScreen.png'}"更多推荐
x:使用空字符串绑定图像
发布评论