Hello,
Through in-depth investigation and official documentation, it is more recommended that this function be easily achieved by binding the same ViewModel to two pages by using the MVVM design method of singleton mode, and the call logic is clear.
Please refer to the following documentation and code samples:
ViewModel:
public class BaseViewModel : INotifyPropertyChanged
{
// create a sington instance.
private static BaseViewModel instance = null;
public static BaseViewModel Instance
{
get
{
if (instance == null)
{
instance = new BaseViewModel();
}
return instance;
}
}
public event PropertyChangedEventHandler PropertyChanged;
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private ObservableCollection<StorePage_Emotes_> storeEmotes;
private ObservableCollection<EmotesModel> emotes;
private StorePage_Emotes_ selectedItem;
public StorePage_Emotes_ SelectedItem
{
get { return selectedItem; }
set
{
selectedItem = value;
OnPropertyChanged(nameof(SelectedItem));
}
}
public ObservableCollection<EmotesModel> Emotes
{
get { return emotes; }
set
{
emotes = value;
OnPropertyChanged(nameof(Emotes));
}
}
public ObservableCollection<StorePage_Emotes_> StoreEmotes
{
get { return storeEmotes; }
set
{
storeEmotes = value;
OnPropertyChanged(nameof(StoreEmotes));
}
}
public ICommand MigrationCommand { get; }
private async void Migration()
{
// add an item from one collection view while also removing an item from another collection
// popup your page in this method.
StoreEmotes.Remove(selectedItem);
Emotes.Add(new EmotesModel { Image = selectedItem.Image });
}
public BaseViewModel()
{
StoreEmotes = new ObservableCollection<StorePage_Emotes_>{
new StorePage_Emotes_ {Name ="No Icon1", Image="dotnet_bot.png"},
new StorePage_Emotes_ {Name ="No Icon2", Image="dotnet_bot.png"},
new StorePage_Emotes_ {Name ="No Icon3", Image="dotnet_bot.png"},
new StorePage_Emotes_ {Name ="No Icon4", Image="dotnet_bot.png"},
new StorePage_Emotes_ {Name ="No Icon5", Image="dotnet_bot.png"}};
Emotes = new ObservableCollection<EmotesModel>();
Emotes.Add(new EmotesModel { Image = "dotnet_bot.png" });
MigrationCommand = new Command(Migration);
}
}
Then, you could bind the viewmodel instance in the code behind of pages.
BindingContext = BaseViewModel.Instance;
Data binding in your two collectionviews:
StoreEmotes CollectionView:
<VerticalStackLayout>
<CollectionView ItemsSource="{Binding StoreEmotes}"
SelectedItem="{Binding SelectedItem}"
SelectionMode="Single"
SelectionChangedCommand="{Binding MigrationCommand}">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding Name}"/>
<Image Source="{Binding Image}"/>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</VerticalStackLayout>
Emotes CollectionView:
<VerticalStackLayout>
<CollectionView ItemsSource="{Binding Emotes}">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Image Source="{Binding Image}"/>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</VerticalStackLayout>
Best Regards,
Alec Liu.
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.