我有一個 ProfilePage.xaml 包含一些像這樣的 ImageButtons(其中六個):
<ImageButton
x:Name="resultImage"
Source="{Binding Profile.Images[0].Source}"
Command="{Binding HandleImage}"
CommandParameter="0">
它們通過我的 ProfilePage.xaml.cs 檔案系結到名為 Profile.cs 的模型,如下所示:
BindingContext = new ViewModels.EditProfilePageViewModel(Profile);
Profile.cs 包含這些影像串列的位置:
List<Image> images = new List<Image>();
public Profile()
{
images.Add(new Image());
images.Add(new Image());
images.Add(new Image());
images.Add(new Image());
images.Add(new Image());
images.Add(new Image());
}
問題是在某些時候我需要使用 ProfilePageViewModel.cs 中的以下代碼進行滑動
for (int i = index; i < Profile.NumberOfImages - 1; i )
{
Profile.Images[i].Source = Profile.Images[i 1].Source;
}
這導致使 List 項指向下一個并一起指向最后一個。我嘗試使用 new string() 等來避免它,但由于使用以下代碼設定 Profile.Images[i].Source 的方式,它沒有用:
var result = await MediaPicker.PickPhotoAsync();
if (result != null)
{
var Text = $"File Name: {result.FileName}";
if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase) ||
result.FileName.EndsWith("jpeg", StringComparison.OrdinalIgnoreCase))
{
var stream = await result.OpenReadAsync();
var source = ImageSource.FromStream(() => stream);
// Add the 'Load Image Icon' on the first available cell
Profile.Images[Profile.NumberOfImages].Source = source;
// Increase the number of photos
Profile.NumberOfImages ;
}
我很確定這種加載影像的方式會破壞系結并造成整個混亂,因為通過硬編碼這些影像源的值不會對系結造成任何問題。所以我提出的問題包括兩件事。關于如何從相機膠卷為 MAUI 加載影像的更好解決方案,而不使用 FFImageLoading(不再起作用)或重置系結的方法。
更新:經過幾天的努力,我使代碼變得簡單。
個人資料.cs:
public class Profile : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
/** --------------------------------------------------------------------
* Properties
* --------------------------------------------------------------------
*/
ObservableCollection<ImageSource> imageSources = new ObservableCollection<ImageSource>();
public ObservableCollection<ImageSource> ImageSources
{
get { return imageSources; }
set { imageSources = value; OnPropertyChanged(nameof(ImageSources)); }
}
public Profile()
{
ImageSources.Add("image1.jpg");
ImageSources.Add("image2.jpg");
ImageSources.Add("image3.jpg");
ImageSources.Add("image4.jpg");
ImageSources.Add("image5.jpg");
ImageSources.Add("image6.jpg");
ImageSources.Add("load_icon.jpg");
}
...
EditProfilePage.xaml:
<ScrollView>
<VerticalStackLayout>
<!-- Photos Grid -->
<Grid HorizontalOptions="Center" Padding="30,40" Margin="0,0" RowSpacing="10" ColumnSpacing="10">
<Grid.RowDefinitions>
<RowDefinition Height="90"></RowDefinition>
<RowDefinition Height="90"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60"></ColumnDefinition>
<ColumnDefinition Width="60"></ColumnDefinition>
<ColumnDefinition Width="60"></ColumnDefinition>
</Grid.ColumnDefinitions>
<!-- Image 1 -->
<ImageButton
x:Name="resultImage"
Source="{Binding Profile.ImageSources[0]}"
Grid.Row="0"
Grid.Column="0"
Aspect="AspectFill"
Grid.RowSpan="1"
Grid.ColumnSpan="1"
Command="{Binding HandleImage}"
CommandParameter="0"
EditProfilePage.xaml.cs
public partial class EditProfilePage : ContentPage
{
public EditProfilePage(Profile Profile)
{
InitializeComponent();
BindingContext = new
ViewModels.EditProfilePageViewModel(Profile);
}
}
EditProfileViewModel.cs:
public EditProfilePageViewModel(Profile Profile)
{
this.Profile = Profile;
HandleImage = new Command(OnHandleImage);
}
void OnPropertyChanged(string propertyName) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
void OnHandleImage(object CommandParameter)
{
// Get the index of the image
int index = int.Parse(CommandParameter.ToString());
// If the index of the selected image is less than the number of the images, delete the selected image
if (index < NumberOfImages-1)
{
DeleteSelectedImage(index);
}
// In case the ImageButton tapped is the one after the last photo
else if (index == NumberOfImages-1)
{
// Then add a new photo at that posiition
LoadPhotoFromLibrary();
}
// In any other case
else
{
// Just do nothing
return;
}
for (int i=0; i<NumberOfImages; i )
{
Console.WriteLine("Image: " i " Src:" Profile.ImageSources[i]);
}
}
void DeleteSelectedImage(int index)
{
Profile.ImageSources.RemoveAt(index);
Profile.ImageSources = Profile.ImageSources;
Console.WriteLine("Deleted: index" index " Number of Image: " NumberOfImages);
}
async void LoadPhotoFromLibrary()
{
// Permsissions
var status = await CheckAndRequestPhotosPermissionAsync();
if (status != PermissionStatus.Granted)
{
// TODO:Notify user permission was denied
return;
}
var result = await MediaPicker.PickPhotoAsync(new MediaPickerOptions
{
Title = "Please pick a photo"
});
var stream = await result.OpenReadAsync();
Profile.ImageSources.Insert(NumberOfImages-1, ImageSource.FromStream(() => stream).ToString());
Profile.ImageSources = Profile.ImageSources;
Console.WriteLine("Number of Image: " NumberOfImages);
}
截至目前,DeleteSelecteImage() 可以很好地處理硬編碼的值。每當我從模擬器加載新影像時,影像通常會顯示在視圖中,但是如果我嘗試從該點洗掉,無論我點擊要洗掉的影像,它都會洗掉最后一個影像。
我需要提到的是,當我從庫中以這種方式上傳影像時會顯示以下錯誤:
[未指定] container_system_group_path_for_identifier: error = ((container_error_t)98) NOT_CODESIGNED [MC] 獲取 systemgroup.com.apple.configurationprofiles 的系統組容器時出錯:98 [MC] 無法獲取組態檔系統組容器路徑。使用預期路徑覆寫:/Users/user/Library/Developer/CoreSimulator/Devices/5C228DCC-D446-423B-BE53-EFE37BDE02A6/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles objc[6750]: Class _PathPoint在 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework 中實作/UIKitCore (0x121945650) 和 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS。平臺/庫/開發人員/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/TextInputUI.framework/TextInputUI(0x16724d690)。將使用兩者之一。哪一個是未定義的。objc[6750]:類 _PointQueue 在 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/ 中都實作了Library/PrivateFrameworks/UIKitCore.framework/UIKitCore (0x121945628) 和 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/系統/庫/PrivateFrameworks/TextInputUI.framework/TextInputUI (0x16724d6b8)。將使用兩者之一。哪一個是未定義的。
uj5u.com熱心網友回復:
根據您更新的代碼,您可以通過使用帶有網格布局的 CollectionView來避免直接訪問單個元素 ( {Binding Profile.ImageSources[0]})的需要。
EditProfilePage.xaml:
<ScrollView>
<CollectionView x:Name="myCollection" ItemsSource="{Binding Profile.ImageSources}"
ItemsLayout="VerticalGrid, 3"
WidthRequest="210" HeightRequest="210" HorizontalOptions="Center" >
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid BackgroundColor="DarkBlue" Padding="5" >
<ImageButton Source="{Binding .}"
Command="{Binding BindingContext.HandleImage, Source={x:Reference myCollection}}"
CommandParameter="{Binding .}" />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ScrollView>
public class EditProfileViewModel
{
...
void OnHandleImage(object CommandParameter)
{
// Get the index of the image
var item = (ImageSource)CommandParameter;
int index = Profile.IndexOf(item);
}
}
public class Profile : ...
{
public ObservableCollection<ImageSource> ImageSources { get; set; } = new ObservableCollection<ImageSource>();
public int IndexOf(ImageSource item)
{
return ImageSources.IndexOf(item);
}
}
解釋:
ItemsLayout="VerticalGrid, 3"將 6 個影像排列在一個網格中。(或者ItemsLayout="HorizontalGrid, 2",對于它們的不同排序。)Profile.ImageSources是ImageSources 的集合。Source={x:Reference myCollection}在項系結中使用,以訪問HandleImageEditProfileViewModel 上的屬性。(從頁面向下傳遞到 CollectionView。)Source="{Binding .}"使用專案本身作為 ImageSource。CommandParameter="{Binding .}"將專案本身(ImageSource)作為引數傳遞。- 使用 找到索引
IndexOf。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/478210.html
標籤:C# xml xamarin xamarin.forms 毛伊岛
