0
votes

I'm new to WP8 and follow many tutorials. For parts of the menu I use a viewModel with NotifyPropertyChanged. When I get my list of news articles it creates a viewModel and displays it in a longListSelector. But also I want to make 1 HubTile with the image and some preview-text of the first article. Is there a nice way to send some event to the .xaml.cs? Or do I have to make another viewModel for this one HubTile and make a binding?

Ony try was to make such a variable: private bool _isDataLoaded = false;

    public bool IsDataLoaded
    {
        get
        {
            return _isDataLoaded;
        }
        set
        {
            if (value != _isDataLoaded)
            {
                _isDataLoaded = value;
                NotifyPropertyChanged("IsDataLoaded");
            }
        }
    }

The same thing is used with "IsLoading"-variable to create a loading-indicator in the systemTray:

void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("MainPage_Loaded-Funktion");


            Binding binding = new Binding("IsLoading") { Source = DataContext };
            BindingOperations.SetBinding(
                prog, ProgressIndicator.IsVisibleProperty, binding);

            binding = new Binding("IsLoading") { Source = DataContext };
            BindingOperations.SetBinding(
                prog, ProgressIndicator.IsIndeterminateProperty, binding);

            prog.Text = "Lade aktuelle Inhalte...";
        }

Can I use this to call a function, when my variable is set and I get a notification?

1
Why are you creating your bindings manually? You should do all binding in XAML. Also, you can create special properties which get filled when the download completes and you can bind HubTile's properties to those view model properties. - Toni Petrina
I'm new to it, that's why I'm doing it wrong :D How do I set such a special property? edit: The LongListSelector is filled automatically, like you said. A binding in the xaml and a viewModel that sends a notification for every change. But what I get is a list of objects. For the HubTile I need a single object or is there a way to say element 0 of that binding in xaml? And beside this HubTile I'd like to know when the download completes. Is there any way? The download happens in the viewModel LoadData()-function. That's far away from the xaml and its cs. - ecth
It is hard to explain everything in just a few characters here, but let me try. You can add properties to your view model directly, just like you did with IsDataLoaded. And you can bind to an array item with the following syntax {Binding Items[0]}. - Toni Petrina
What makes me wonder is that for the longListSelector i do: <phone:LongListSelector ItemSource="{Binding OnlineNews}"> <TextBlock Text="{Binding TeaserText}"/> </phone:LongListSelector>. First I tell the compiler which Model: "OnlineNews" and then I say which Value of Element x for the x'th row. In my case with the HubTile I tried <toolkit:HubTile Message="{Binding OnlineNews[0].TeaserText}"/> It doesn't crash but also doesn't do anything else :( // You're free to post a "big" comment. Otherwise I can't check your answer as valid solution ;) - ecth
Okay, lol, now it works. Just tested it, to be sure not to ask again without reason and it didn't. So yes: If anybody needs a solution - this is one. Thanks! - ecth

1 Answers

0
votes

The solution that helped me out was this:

<toolkit:HubTile Message="{Binding OnlineNews[0].TeaserText}"/>

Didn't know that you can access the viewModel like that. Thanks to Toni Petrina!