Calling a method within the Model once the Screen has completed loading

Discussion in 'Plugin Development' started by Valk, March 24, 2013.

  1. Valk

    Valk Portal Pro

    Joined:
    February 25, 2006
    Messages:
    302
    Likes Received:
    93
    Ratings:
    +138 / 0
    Home Country:
    Australia Australia
    Show System Specs
    I'm having a little trouble. I want to call a method after the Screen has fully loaded.

    Reason been I'm going to call

    var screenManager = ServiceRegistration.Get<IScreenManager>();

    screenManager.ShowDialog("dialogWeatherSearchResult");

    If certain conditions are met.

    At the moment the best hack I can think of goes something along the lines of:



    <ControlTemplate.Triggers>
    <DataTrigger Binding="{Binding Loaded}" Value="False">
    <!--<Command Source="{StaticResource Model}" Path="DetectLocation"/>-->
    <Setter Property="{Binding Loaded}" Value="True"/>
    </DataTrigger>
    </ControlTemplate.Triggers>

    Note the above doesn't work but the idea was to set a property and use that to trigger the function in the code behind (sharp learning curve with XAML at the moment).

    I've been focusing of the XAML because I can't see a IWorkflowModel function that is called after the Screen has focus.

    Any advice / thoughts would be appreciated.
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Valk

    Valk Portal Pro

    Joined:
    February 25, 2006
    Messages:
    302
    Likes Received:
    93
    Ratings:
    +138 / 0
    Home Country:
    Australia Australia
    Show System Specs
    In my quest I decided to dig deeper into MP2. As I was going through I came across IWorkflowModel.cs and noticed a small TODO:

    // TODO: Create methods Activate and Deactivate to execute initialization/deinitialization like message queue setup etc.

    // EnterModelContext and ExitModelContext can then be replaced by ChangeModelContext.

    While I didn't implement ChangeModelContext I did add an void Activate(NavigationContext currentContext); function to IWorkflowModel.cs and implemented it in WorkflowManager.cs at the end of the function UpdateScreen_NeedsLock(bool push, bool force)

    After that I found I was able to get my dialog to appear but while the thread is working on the first load of the form the side menu expands until the dialog is shown.

    At the moment I'm not sure what's causing the menu to expand in the first place but it's causing me grief.

    So I guess the follow up question is how did @Albert I guess intend the Activate function to work so I can do this properly? ;)
     
    • Like Like x 1
  4. morpheus_xx
    • Team MediaPortal

    morpheus_xx Lead Dev MP2

    Joined:
    March 24, 2007
    Messages:
    10,908
    Likes Received:
    4,701
    Ratings:
    +6,738 / 11
    Home Country:
    Germany Germany
    Show System Specs
    Hi @Valk,

    did you push your changes already, so I can take a look at it? I don't fully understand the problem yet.

    Are you talking about Titanium skin? If you look at Titanium\screens\master_menu.xaml you'll find:
    Code (XML):
    1.           <Grid.Triggers>
    2.             <!-- Setting default state of menu to hidden -->
    3.             <EventTrigger RoutedEvent="Screen.Show">
    4.               <TriggerCommand Command="{Command Source={StaticResource MenuModel}, Path=CloseMenu}"/>
    5.             </EventTrigger>
    6.  
    7.             <!-- Slide-In animation when the MenuModel.ShowMenu gets true -->
    8.             <DataTrigger Binding="{Binding Source={StaticResource MenuModel},Path=IsMenuOpen}" Value="True">
    9.  
    On "Screen.Show" event the "CloseMenu" is invoked, it will hide the menu. Hiding itself is done in DataTrigger.

    Morpheus
     
  5. Valk

    Valk Portal Pro

    Joined:
    February 25, 2006
    Messages:
    302
    Likes Received:
    93
    Ratings:
    +138 / 0
    Home Country:
    Australia Australia
    Show System Specs
    Sorry I haven't pushed the latest version yet (I'll do it shortly).

    All I'm trying to do is call DetectLocation() when someone enters the weather.xaml screen.

    I looked over triggers but didn't find EventTrigger. That would do all I need looking at your example (I didn't read master_menu.xaml).

    That said I'm pretty sure I know why the menu is expanded after that code snippet you posted. I call Activate before

    WorkflowManagerMessaging.SendNavigationCompleteMessage();
    is called so the Event Trigger won't have happened yet. So I guess is that a good behaviour? The first quick solution I came up with was to call DetectLocation() Aysnc but I may even change it to an EventTrigger.
     
    • Like Like x 1
  6. Valk

    Valk Portal Pro

    Joined:
    February 25, 2006
    Messages:
    302
    Likes Received:
    93
    Ratings:
    +138 / 0
    Home Country:
    Australia Australia
    Show System Specs
    Tried EventTrigger and it calls my trigger first so I get the same undesired effect if I call synchronously. If I make the call asynchronously I can get around the issue, it just means I have to make sure the context is still active or add a cancelation token before it shows any dialogs.

    Personally, I like the Activate call over using EventTrigger so for the moment I'll stick to using it.

    Only real issue remaining is the dialogs. The dialogs I wish to reuse are set to use the WeatherSetupModel instead of the WeatherModel, is there a way to make them support either Models or do I just have to copy them?

    Oh and I remembered to push the changes this time :D .
     
    Last edited: March 27, 2013
  7. morpheus_xx
    • Team MediaPortal

    morpheus_xx Lead Dev MP2

    Joined:
    March 24, 2007
    Messages:
    10,908
    Likes Received:
    4,701
    Ratings:
    +6,738 / 11
    Home Country:
    Germany Germany
    Show System Specs
    I think detection of location is better placed inside WeatherSetupModel, so there should be no need to change the dialog's model reference.

    If reallllly required, you can access multiple models from screen, just add a <Model..> XAML resource definition and use it in binding accordingly. There are many examples in existing xaml pages.

    I will be away for the next week and can check code changes when I'm back.

    Thanks again for your work!
     
  8. Valk

    Valk Portal Pro

    Joined:
    February 25, 2006
    Messages:
    302
    Likes Received:
    93
    Ratings:
    +138 / 0
    Home Country:
    Australia Australia
    Show System Specs
    The only reason I have it in the WeatherModel also is for the first time triggering. I guess the other way to do it would be automatically navigate to the setup screen if no settings are defined.

    Also thanks for the help.
     
    Last edited: March 27, 2013
    • Like Like x 1
Loading...

Users Viewing Thread (Users: 0, Guests: 0)

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice
  • About The Project

    The vision of the MediaPortal project is to create a free open source media centre application, which supports all advanced media centre functions, and is accessible to all Windows users.

    In reaching this goal we are working every day to make sure our software is one of the best.

             

  • Support MediaPortal!

    The team works very hard to make sure the community is running the best HTPC-software. We give away MediaPortal for free but hosting and software is not for us.

    Care to support our work with a few bucks? We'd really appreciate it!