Hello,
Welcome to our Microsoft Q&A platform!
I have to say you shouldn't store raw file paths if you want to avoid this issue. A better practice would be to only store the relative part of the path and always attach it to the current "root" path. You could have a try with the following code:
Change the LoadPhotoAsync
method by replacing PhotoPath
.
async Task LoadPhotoAsync(FileResult photo)
{
..........
var newFile = Path.Combine(FileSystem.AppDataDirectory, photo.FileName);
using (var stream = await photo.OpenReadAsync())
using (var newStream = File.OpenWrite(newFile))
await stream.CopyToAsync(newStream);
PhotoPath = photo.FileName;
............
}
When you display the image, you need to combine the path.
AvatarImage.Source = Path.Combine(FileSystem.AppDataDirectory, PhotoPath);
I also add a CombineOperatorAvatar
property to OperatorModel
, and change the ItemsSource
of OperatorListView and databing to test the effect , it works.
Model
public string CombineOperatorAvatar { get; set; }
SelectOperatorPage
protected override async void OnAppearing()
{
base.OnAppearing();
HFNDatabase database = await HFNDatabase.Instance;
List< OperatorModel>dataList = await database.GetOperatorsAsync();
foreach (var OperatorModel in dataList)
{
OperatorModel.CombineOperatorAvatar = Path.Combine(FileSystem.AppDataDirectory, OperatorModel.OperatorAvatar);
}
OperatorListView.ItemsSource = dataList;
}
XAML
<Image
Source="{Binding CombineOperatorAvatar}"
Grid.Row="0"
Grid.Column="0"
Margin="5"
HeightRequest="100"
WidthRequest="100"/>
Best Regards,
Wenyan Zhang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.