简体   繁体   中英

View is reloaded after Remote Desktop Connection (RDP) in WPF

In WPF MVVM application, UserControls are reloaded after Remote Desktop Connection to pc that appplication is running, after that we got stuck in some problems.I read this link about WPF , but I cannot resolve this issue.

There is an ItemsControl that UserControl s are added to:

<ItemsControl ItemsSource="{Binding CamerasList}" x:Name="AllCamerasControl">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <local:SingleView />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

The constructor of the UserControl is as follows:

public SingleCameraView()
{ 
    InitializeComponent();
    DataContext = new SingleViewModel();
}

Can anyone tell me how I can resolve this issue?

Elsewhere it has been noted that the way to handle the unload/load two-step that occurs when connecting to a system that is running a WPF app, and that to avoid issues you should guard against reentry of the initialization code in your "Loaded" handlers, so i'm not getting into that subject here.

What I have found recently is that when this occurs the controls retain their DataContract value, for the most part.

That said I uncovered a wrinkle recently regarding ItemsControl.

It appears that as part of the layout/render/reload pass the ItemsControl is reassessed completely, and when this occurs the templated-in elements are completely recreated and re-bound.

What this means for your sample above is that as part of the unload/load processing, the existing UniformGrid is removed and has its DataContract un-assigned, and an entirely new instance is created, and it gets the DataContract assigned.

Following that the "CamerasList" is rebound to ItemsSource, and the repeating ItemTemplate is applied for each item in the list.

I don't know from your provided sample whether this is specifically a problem for you. If you are following the MVVM pattern this will usually sort itself out, but if you have any code-behind in the views, it may not properly account for being swapped out and replaced.

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