简体   繁体   中英

WPF UserControl- pre-Load UserControl

I have a MainWindow and 4 UserControls. By switching the DataContext to my UserControls I can have an application with multiple "Pages". In every UserControl I have a webBrowser-Control that Displays an PowerPoint (so -> 4 UC = 4 ppt). The issue I have now is that when I Switch my DataContext (Switch Page) I have to load (call the navigate Method) the whole ppt in my webBrowser again and that takes some Time. How can I fix this?

thanks in advance :))

Adrian

EDIT CODE

MainWindow.xaml

<Window.Resources>
    <DataTemplate x:Name="Page1Template" DataType="{x:Type viewmodels:Page1Model}" >
        <views:Page1 DataContext="{Binding}"/>
    </DataTemplate>
    <DataTemplate x:Name="Page2Template" DataType="{x:Type viewmodels:Page2Model}">
        <views:Page2 DataContext="{Binding}"/>
    </DataTemplate>
    <DataTemplate x:Name="Page3Template" DataType="{x:Type viewmodels:Page3Model}">
        <views:Page3 DataContext="{Binding}"/>
    </DataTemplate>
    <DataTemplate x:Name="Page4Template" DataType="{x:Type viewmodels:Page4Model}">
        <views:Page4 DataContext="{Binding}"/>    
</Window.Resources>

// ...

<ContentControl Content="{Binding}"></ContentControl>

MainWindow.xaml.cs (i call the page Switch like this)

 private void menuBtn1_Click(object sender, RoutedEventArgs e)
 {   
    DataContext = new Page1Model();   
 }
 private void menuBtn2_Click(object sender, RoutedEventArgs e)
 {
    DataContext = new Page2Model();
 }
 private void menuBtn3_Click(object sender, RoutedEventArgs e)
 {
    DataContext = new Page3Model();
 }
 private void menuBtn4_Click(object sender, RoutedEventArgs e)
 {
    DataContext = new Page4Model();
 }

and lets say eg my UserControl1: (when i call UC1 every time the ppt is opening again, i want just to open it one time):

private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        powerPointBrowser1.Navigate("somePPTfile.pptx");
        powerPointBrowser1.LoadCompleted += powerPointBrowser1_LoadCompleted;
    }

i hope i made it clear :S

In Mainwindow.xaml Place the view in stackpanel to make visiblity hide show like this.

<DataTemplate x:Name="Page1Template" DataType="{x:Type viewmodels:Page1Model}">
 <StackPanel Visibility="{Binding Page1}">
  <views:Page1 DataContext="{Binding}"/>
 </StackPanel>
</DataTemplate>

In MainWindow.xaml.cs you have to set a property for visibility like this.

private Visibility page1;
public Visibility Page1
{
    get { return page1; }
    set { page1 = value; }
}

Then initialize the DataContext of each view in your MainWindowLoad function, so that it will be preloaded. After that you can set the visibility for the each view in your each menu click function like this: Page1 = Visibility.Visible; or Page1 = Visibility.Collapsed; I hope it will works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM