嗨,我有一个xaml文件,其中我有一个NavigationView和NavigatioViewItems。在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方法应该以通用的方式实现这一点,因为整个应用程序都会使用它。我将有不同的窗口,如上面的xaml文件,我想将视图注入到ScrollViewer框架中。是否有一种方法可以在代码中获得对此ScrollViewer的引用,然后将其框架设置为我要通过NavigationService指定的视图?任何帮助都是非常感谢的。谢谢
发布于 2021-06-24 03:10:13
C# UWP MVVM在另一个视图中注入一个视图
如果使用了MVVM设计,则需要将框架SourcePageType属性与ViewModel的页面类型属性绑定。您只需更新此页面类型属性,框架内容将被更新。
这里是您可以参考的示例代码。
是否有一种方法可以在代码中获得对此ScrollViewer的引用,然后将其框架设置为我要通过NavigationService指定的视图?
另一种方法是将ContentFrame实例传递给NavigationService。首先,您需要为NavigationService类添加新的框架属性。然后将代码框架替换为MyFrame。
public class NavigationService
{
private Frame MyFrame { get; set; }
public NavigationService(Frame frame)
{
MyFrame = frame;
}
}Usgae
var navService = new NavigationService(ContentFrame);对于上面的设计是指Windows模板Studio NavigationService,它是完整的实现,您可以在shell页面中找到NavigationService初始化过程。
ViewModel.Initialize(shellFrame, navigationView, KeyboardAccelerators);https://stackoverflow.com/questions/68103994
复制相似问题