Jeong Yo Han
Jeong Yo Han

Reputation: 600

where should I put my data that will not be discarded while programs on In MVVM

I have a experience with devel UWP and code behind structure.
I'm new to MVVM design, and I'm developing UWP App with Template, MVVM Basic.

I'm developing a simple App that generate numbers for lotto. To help understanding, I posted my App picture.
my App

I implemented with ViewModel with main page. below is my source.

In Mainpage.Xaml

<ListView ItemsSource="{x:Bind ViewModel._display_game, Mode=OneWay}">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="TextBlock">
            <TextBlock Text="{x:Bind Text,Mode=OneWay}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
<Button Grid.Column="1" Grid.Row="1" Content="Click Me" Command="{x:Bind ViewModel.game_create}"/>

MainPage.cs

public sealed partial class MainPage : Page
{
    public MainViewModel ViewModel { get; } = new MainViewModel();

    public MainPage()
    {
        DataContext = ViewModel;
        InitializeComponent();
    }
}

MainViewModel.cs

public class MainViewModel : Observable
{
    public ObservableCollection<TextBlock> _display_game { get; set; }

    private int[] game;
    public MainViewModel()
    {
        game_create = new RelayCommand(ExecuteClick);
    }
    private void ExecuteClick()
    {
        {  //generating 6 random value 
            _display_game = new ObservableCollection<TextBlock>();
            game = new int[6];
            Random random = new Random(DateTime.Now.Millisecond);
            for (int t = 0; t < 6; t++)
            {
                bool[] check = new bool[46];
                _display_game.Add(new TextBlock());

                int dup = 0;
                int cnt = 0;
                while (cnt < 6)
                {
                    int tmp = random.Next(1, 46);
                    if (check[tmp] == false)
                    {
                        check[tmp] = true;
                        game[cnt] = tmp;
                        cnt++;
                        //_display_game[t].Text += $"{tmp} ";
                    }
                    else dup++;
                }
                Array.Sort(game);
                _display_game[t].Text += string.Format("{0} 게임 : ", t+1);
                for (int i = 0; i < 6; i++) _display_game[t].Text += $"{game[i]} ";
            }
        }
        OnPropertyChanged("_display_game");
    }
    public RelayCommand game_create { get; set; }
}

and the question is here. as I know, ViewModel is a part of View. So, I navigated Page, Listview Items are refreshed(I mean they are gone). but I want to make them alive whatever user does, except user clicks fresh button.

I want to make my data available even I'm out of page and comeback. and I want use this data at another page also.

when I do code behind(I think I don't have any pattern before), I put the any codes to anywhere to achieve my goal. if I want to use this page's data at another page, then I just passed list as parameters to another page's overloaded OnNavigated Method.

but, I'm new to MVVM, and I often become afraid of myself violating MVVM rules. so I post this question.

I'm sorry for long question.

Upvotes: 0

Views: 45

Answers (1)

Muhammad Touseef
Muhammad Touseef

Reputation: 4465

There are multiple ways you can do it but first I will tell you the way you already know so you are comfortable with it.

You can still pass the list of data to next page as parameter when you navigate to the next page. the only difference is before in code behind you used to send ** _display_game** directly as parameter now you just need to send ViewModel. _display_game as parameter and remaining logic on your OnNavigatedTo on next page will remain same as before, and on next page you can receive that parameter and even set to the

ViewModel.listofNextPage = e.Parameter as ObservableCollection<TextBlock> 

but that depends what you want to do on your next page.

Now I will just give a little hints of other possible methods so that you know your options.

you can use NavigationCacheMode property of the page to Required and this way the data on that page will remain saved even if you go to other pages and come back.

Another way can be using a Static Class as a service which will hold the collection of your data. and that collection will also be static this way you can access that collection of data anywhere in your application.

Tip : Do not use TextBlock as the type in your collection instead just use string if you need only 1 property or make a custom class like following :

class MyClass
{
    public string Text { get ; set ; }
}

then make collection of this type as following :

public ObservableCollection<MyClass> _display_game { get; set; }

change the datatype of your itemtemplate :

<DataTemplate x:DataType="MyClass">

Because TextBlock is a UI control so it is best that it is used for that purpose only, and for databinding just use normal classes.

Upvotes: 1

Related Questions