In XAML I have the following line:
<Image x:Name="MainImage"
Source="{x:Bind ViewModel.MainPic,Mode=OneWay,TargetNullValue={x:Null}}"
Stretch="UniformToFill"/>
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: The parameter is incorrect.
Debugger breaks at:
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);
}
}
Apparently, this occurs since MainPic is returning null.
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?
UPDATE:
Thanks to the users who answered, following conclusion is drawn (for UWP):
- 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.
Here's the converter code:
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));
}