I have to use an Image for GridViewItem
style, every GridViewItem
represents an object saved in a SQLite database, when I save a new object into the db I have to select from a combobox which image it uses.
This is the definition of the Combobox:
<ComboBox x:Name="UriImage" Width="200" RelativePanel.Below="ActiveToggle">
<ComboBoxItem Content="Element1" x:Name="element1" IsSelected="True"/>
<ComboBoxItem Content="Element2" x:Name="element2"/>
<ComboBoxItem Content="Element3" x:Name="element3"/>
</ComboBox>
Then I created this to match the corresponding URI of the image to each ComboBoxItem
string ImageUriString="";
if (UriImage.SelectedValuePath == "Element1")
ImageUriString = "ms-appx:///Assets/Orchid_2_FF.png";
else if (UriImage.SelectedValuePath == "Element2")
ImageUriString = "ms-appx:///Assets/sapphire_orchid_FF.png";
else if (UriImage.SelectedValuePath == "Element3")
ImageUriString = "ms-appx:///Assets/yellow-cowslip-orchid_FF.png";
Uri UriString = new Uri(ImageUriString, UriKind.RelativeOrAbsolute);
Then I added the Uri
field to the class that represents the object when I try to save new object I have this Error: Don't know how to read System.Uri
To load the objects in the Gridview I use this:
private async void ReadAllSystemList_Loaded(object sender, RoutedEventArgs e)
{
ReadAllSystemsList dbSystems = new ReadAllSystemsList();
DB_FFSystems = await dbSystems.GetAllFFSystems(); //get all DB Systems
listBoxObj.ItemsSource = DB_FFSystems.OrderByDescending(i => i.ID).ToList();
}
and data Binding from Xaml.
<Image Width="350" Height="200" x:Name="ElementImage" Source="{Binding ImageUri}">
I thought about saving the URI as a string but how can I convert the string to Uri when loading the Gridview?
SQLite cannot store and parse URIs so that is the source of the error you are getting.
You can safely store string
instead of Uri
in the database. Usually the ideal approach is to have a simple class that is used as a database model (having a string
property) and then another class that is used in the presentation layer and that will have Uri
instead. This way you get a good separation of layers and can have a more practical interface on the presentation layer that is not limited by the database. You can even use libraries like Automapper
to easily convert from one type to another instead of having to do it property by property.
However, if you don't want to deal with this, there are other options. First of all - you shouldn't have to convert from string
to Uri
at all when binding to Source
of a Image
. There is an implicit type conversion from string
to Uri
built into XAML, so it should work as expected.
Alternatively, you could implement a converter by creating a class deriving from IValueConverter
. This whould override the Convert
method and build a Uri
instance from the passed in string
. Then you register the converter as a resource with a x:Key
and use it in code:
Source="{Binding ImageUri, Converter={StaticResource StringToUriConverter}"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.