Home > Software design >  Passing data from viewmodel to viewmodel and use it .Net maui
Passing data from viewmodel to viewmodel and use it .Net maui

Time:06-27

How to use viewmodel get data and draw graphic,Can I send data from mainpageviewmodel to use on graphicviewmodel and show on graphicpage

MainPage.xaml

<CollectionView
            BackgroundColor="Transparent"
            ItemsSource="{Binding graphiclist}"
            SelectionMode="None">

    <CollectionView.ItemTemplate>
        <DataTemplate x:DataType="model:GRAPHICModel">
            <Grid Padding="0">
                <Frame HeightRequest="100">
                    <Frame.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:MainViewModel}}, Path=DrawGRAPHCommand}" CommandParameter="{Binding .}" />
                    </Frame.GestureRecognizers>
                    <Grid Padding="0">
                        <VerticalStackLayout>
                            <Label Style="{StaticResource BoldLabel}" Text="{Binding GraphicNamae}" />
                        
                        </VerticalStackLayout>
                    </Grid>
                </Frame>
            </Grid>

        </DataTemplate>
    </CollectionView.ItemTemplate>

mainpageviewmodel.cs

public partial class MainpageViewModel : ObservableObject
{
        public ObservableCollection<GRAPHICModel> graphiclist { get; set; }
        public MainpageViewModel()
        {
            graphiclist = new ObservableCollection<GRAPHICModel>
            {
                new GRAPHICModel { GraphicNamae= "Name1", Path = "somecoordinate" },
                new GRAPHICModel { GraphicName= "Name2", Path = "somecoordinate" },
                new GRAPHICModel { GraphicName= "Name3", Path = "somecoordinate" },
                new GRAPHICModel { GraphicName= "Name4", Path = "somecoordinate" }
            };
        }

    [ICommand]
    async Task DrawGRAPHCommand(GRAPHICModel glist)
    {
       
        await Shell.Current.GoToAsync(nameof(DrawPage), true, new Dictionary<string, object>
        {
            ["GRAPHICModel"] = glist
        });
    }
}

graphicpage.xaml

<VerticalStackLayout>
    <Label Text="{Binding Graphiclist.Country }" />
    <FlexLayout
        AlignItems="Center"
        Direction="Column"
        JustifyContent="SpaceEvenly">

        <VerticalStackLayout>
            <GraphicsView
                Drawable="{StaticResource drawable}"
                HeightRequest="1000"
                WidthRequest="1000" />
        </VerticalStackLayout>
    </FlexLayout>

</VerticalStackLayout>

graphicviewmodel.cs

[QueryProperty(nameof(Graphiclist), "GRAPHICModel")]

public partial class GraphicViewModel : ObservableObject, IDrawable
{
    [ObservableProperty]
    GRAPHICModel graphiclist;

    public void Draw(ICanvas canvas, RectF dirtyRect)
    {
        something.load(graphiclist.Path)
         canvas.StrokeColor = Colors.Red;
            canvas.StrokeSize = 4;
            canvas.DrawPath();
    }
}

After tap , can not get graphiclist.path for canvas.DrawPath(-somepoint-); after tap

but can show <Label Text="{Binding Graphiclist.Country }" />

CodePudding user response:

In MVVM for communication between View Models common practice is to use Mediator Design Pattern !! There are a lot articles around it as much as yt videos.

CodePudding user response:

You can pass data during navigation.

If your data may be too big, you could buffer it in a global static class and just pass an ID or something else to find it again from within the other viewmodel.

  • Related