x:用空字符串绑定图像 [英] x:Bind image with null string
问题描述
在 XAML 中,我有以下几行:
In XAML I have the following line:
<Image x:Name="MainImage"
Source="{x:Bind ViewModel.MainPic,Mode=OneWay,TargetNullValue={x:Null}}"
Stretch="UniformToFill"/>
在视图模型中:
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 提到的任何方法.给我,更改所有 vm 以停止返回 null 很难.所以添加一个简单的转换器到 xaml 也能解决问题.
- If you're binding image source to a
string
, it cannot benull
or empty""
. A singe character"x"
or a space" "
would work. - If you bind to a
BitmapImage
, returningnull
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<的
Source
/code> 需要绑定到完全相同类型的属性 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='http://Assets/SplashScreen.png' TargetNullValue='http://Assets/SplashScreen.png'}"
这篇关于x:用空字符串绑定图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!