C# UWP MVVM 在另一个视图中注入一个视图
C# UWP MVVM inject a view inside another view
您好,我有一个 xaml 文件,其中有一个带有 NavigatioViewItems 的 NavigationView。在 NavigationView 中,我有一个 ScrollViewer,我想在其中注入一个视图。这是 xaml:
<Page
x:Class="ToolBoxApp.Views.AudioHomeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ToolBoxApp.Views"
xmlns:viewmodels="using:ToolBoxApp.ViewModels"
xmlns:mainview="clr-namespace:ToolBoxApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<!--<Page.DataContext>
<viewmodels:AudioHomeViewModel/>
</Page.DataContext>-->
<Grid>
<NavigationView x:Name="navigationViewControl"
IsBackEnabled="true"
>
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="ItemInvoked">
<core:EventTriggerBehavior.Actions>
<core:InvokeCommandAction Command="{Binding NavigateToTextToSpeechView}" />
</core:EventTriggerBehavior.Actions>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
<NavigationView.MenuItems>
<NavigationViewItem Icon="MusicInfo" Content="Text to Speech"/>
<NavigationViewItem Icon="MusicInfo" Content="Youtube to Mp3"/>
</NavigationView.MenuItems>
<ScrollViewer>
<Frame x:Name="ContentFrame"/>
</ScrollViewer>
</NavigationView>
</Grid>
</Page>
到目前为止,我有一个 NavigationService,我可以在其中导航到不同的视图:
public class NavigationService : INavigationService
{
public void GoBack()
{
var frame = (Frame)Window.Current.Content;
frame.GoBack();
}
public void Navigate(Type sourcePage)
{
var frame = (Frame)Window.Current.Content;
frame.Navigate(sourcePage);
}
public void Navigate(Type sourcePage, object parameter)
{
var frame = (Frame)Window.Current.Content;
frame.Navigate(sourcePage, parameter);
}
public void NavigateScrollViewer(Type sourcePage, Type injectPage)
{
var frame = (Frame)Window.Current.Content;
var page = frame.CurrentSourcePageType;
}
}
NavigatScrollViewer 方法应该以通用方式实现它,因为整个应用程序都将使用它。我将有不同的 Windows,如上面的 xaml 文件,我想在其中将视图注入 ScrollViewer Frame。有没有办法在代码中获取对此 ScrollViewer 的引用,然后将其 Frame 设置为我想通过 NavigationService 指定的视图?
任何帮助表示赞赏。谢谢
C# UWP MVVM inject a view inside another view
如果使用过MVVM设计,需要绑定FrameSourcePageType
属性与ViewModel的页面类型属性。您只需要更新此页面类型属性,框架内容就会更新。
Here 是您可以参考的示例代码。
Is there a way to get a reference to this ScrollViewer inside code and then setting its Frame to the view I want to specify through the NavigationService?
另一种方法是将 ContentFrame
实例传递给 NavigationService
。首先,您需要为 NavigationService class 添加新框架 属性。然后用 MyFrame
.
替换你的代码框
public class NavigationService
{
private Frame MyFrame { get; set; }
public NavigationService(Frame frame)
{
MyFrame = frame;
}
}
乌斯佳
var navService = new NavigationService(ContentFrame);
以上设计参考了Windows Template StudioNavigationService
,是完整的实现,NavigationService
初始化过程在shell页面[=23] =]
ViewModel.Initialize(shellFrame, navigationView, KeyboardAccelerators);
您好,我有一个 xaml 文件,其中有一个带有 NavigatioViewItems 的 NavigationView。在 NavigationView 中,我有一个 ScrollViewer,我想在其中注入一个视图。这是 xaml:
<Page
x:Class="ToolBoxApp.Views.AudioHomeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ToolBoxApp.Views"
xmlns:viewmodels="using:ToolBoxApp.ViewModels"
xmlns:mainview="clr-namespace:ToolBoxApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<!--<Page.DataContext>
<viewmodels:AudioHomeViewModel/>
</Page.DataContext>-->
<Grid>
<NavigationView x:Name="navigationViewControl"
IsBackEnabled="true"
>
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="ItemInvoked">
<core:EventTriggerBehavior.Actions>
<core:InvokeCommandAction Command="{Binding NavigateToTextToSpeechView}" />
</core:EventTriggerBehavior.Actions>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
<NavigationView.MenuItems>
<NavigationViewItem Icon="MusicInfo" Content="Text to Speech"/>
<NavigationViewItem Icon="MusicInfo" Content="Youtube to Mp3"/>
</NavigationView.MenuItems>
<ScrollViewer>
<Frame x:Name="ContentFrame"/>
</ScrollViewer>
</NavigationView>
</Grid>
</Page>
到目前为止,我有一个 NavigationService,我可以在其中导航到不同的视图:
public class NavigationService : INavigationService
{
public void GoBack()
{
var frame = (Frame)Window.Current.Content;
frame.GoBack();
}
public void Navigate(Type sourcePage)
{
var frame = (Frame)Window.Current.Content;
frame.Navigate(sourcePage);
}
public void Navigate(Type sourcePage, object parameter)
{
var frame = (Frame)Window.Current.Content;
frame.Navigate(sourcePage, parameter);
}
public void NavigateScrollViewer(Type sourcePage, Type injectPage)
{
var frame = (Frame)Window.Current.Content;
var page = frame.CurrentSourcePageType;
}
}
NavigatScrollViewer 方法应该以通用方式实现它,因为整个应用程序都将使用它。我将有不同的 Windows,如上面的 xaml 文件,我想在其中将视图注入 ScrollViewer Frame。有没有办法在代码中获取对此 ScrollViewer 的引用,然后将其 Frame 设置为我想通过 NavigationService 指定的视图? 任何帮助表示赞赏。谢谢
C# UWP MVVM inject a view inside another view
如果使用过MVVM设计,需要绑定FrameSourcePageType
属性与ViewModel的页面类型属性。您只需要更新此页面类型属性,框架内容就会更新。
Here 是您可以参考的示例代码。
Is there a way to get a reference to this ScrollViewer inside code and then setting its Frame to the view I want to specify through the NavigationService?
另一种方法是将 ContentFrame
实例传递给 NavigationService
。首先,您需要为 NavigationService class 添加新框架 属性。然后用 MyFrame
.
public class NavigationService
{
private Frame MyFrame { get; set; }
public NavigationService(Frame frame)
{
MyFrame = frame;
}
}
乌斯佳
var navService = new NavigationService(ContentFrame);
以上设计参考了Windows Template StudioNavigationService
,是完整的实现,NavigationService
初始化过程在shell页面[=23] =]
ViewModel.Initialize(shellFrame, navigationView, KeyboardAccelerators);