Reputation: 1319
I have a Windows Phone 8.1 application which loads different images based on the resolution (scale factor).
I have added the reference at the top
xmlns:converters="using:MyApp.Converters"
I have added the converter in the static resources of the page
<converters:ResolutionConverter x:Key="resolutionLogoConverter"
HD720p="/Assets/Logo/About.logo-WXGA.png"
WXGA="/Assets/Logo/About.logo-WXGA.png"
WVGA="/Assets/Logo/About.logo-WVGA.png" />
I have added the image in my page. However the converter never gets called and there will be nothing loaded on the page in the place where the image is supposed to load.
<Image Source="{Binding Converter={StaticResource resolutionLogoConverter}}"
Margin="0,10,0,0" />
Here's my converter
public class ResolutionConverter : IValueConverter
{
public object HD720p { get; set; }
public object WXGA { get; set; }
public object WVGA { get; set; }
public object Convert(object value, Type targetType, object parameter, string language)
{
switch (ResolutionHelper.CurrentResolution)
{
case ResolutionHelper.Resolutions.HD720p:
return HD720p;
case ResolutionHelper.Resolutions.WVGA:
return WVGA;
case ResolutionHelper.Resolutions.WXGA:
return WXGA;
default:
throw new NotSupportedException("Unsupported resolution type");
}
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
When I put a breakpoint in the first line of the Convert function, it is not hitting the breakpoint. Am I doing something wrong. I would be very glad if someone can point me in the right direction.
Thanks in advance.
Upvotes: 2
Views: 370
Reputation: 4292
There is not need to use converter for this case. As now in windows phone 8.1 universal app image different images for different resolution is handled by the OS. and also OS will download only the required resolution image on the device(instead of all images) What you have to is to name images differently... see this post
Upvotes: 3
Reputation: 930
I think the main problem is not the converter, maybe your Binding
is not properly set, because I see that you are not binding to a property path (although it's not mandatory). If you are not familiar with the Binding markup extension, you can check Binding markup extension or Data binding overview.
Also, you might try something like:
<Image x:Name="myImage" Source="{Binding Converter={StaticResource resolutionLogoConverter}}"
Margin="0,10,0,0" />
and in code-behind:
this.myImage.DataContext = ResolutionHelper.CurrentResolution;
and change in your converter switch (ResolutionHelper.CurrentResolution)
with switch ((ResolutionHelper.Resolution)object)
Upvotes: 0