我正在测试一些东西但是......我陷入了困境。 我测试了许多东西,看着网络,但没有。 在MainPage.xaml中:
<SplitView.Content>
<Frame Name="MainPageMyFrame"></Frame>
</SplitView.Content>
在MainPage的.cs中:
private void ListView_ItemClick(object sender, ItemClickEventArgs e)
{
if (MainPageListView.SelectedIndex == 0)
{
MainPageMyFrame.Navigate(typeof(Pages.MainWindow));
}
}
每次我点击项目ID 0;即时通讯,当它想要导航时:
Exception thrown: 'System.NullReferenceException'
MainWindow是UserControl。 有谁知道如何解决这个错误?
@Update MainPage.xaml中
<SplitView Grid.Row="1" Name="MainPageSplitView" OpenPaneLength="200" CompactPaneLength="50" DisplayMode="CompactOverlay">
<SplitView.Pane>
<ListView Name="MainPageListView" Background="LightGray" ItemClick="ListView_ItemClick" IsItemClickEnabled="True">
<ListView.Items>
<ListViewItem BorderThickness="0,1,0,1" BorderBrush="DarkGray">
<StackPanel Orientation="Horizontal">
<TextBlock Foreground="Black" FontFamily="Segoe MDL2 Assets" Text="" FontSize="25" />
<TextBlock Margin="15, 0" Foreground="Black" FontSize="20" VerticalAlignment="Center" Text="Rozkład jazdy" />
</StackPanel>
</ListViewItem>
<ListViewItem BorderThickness="0,0,0,1" BorderBrush="DarkGray">
<StackPanel Orientation="Horizontal">
<TextBlock Foreground="Black" FontFamily="Segoe MDL2 Assets" Text="" FontSize="25" />
<TextBlock Margin="15, 0" Foreground="Black" FontSize="20" VerticalAlignment="Center" Text="Komunikaty" />
</StackPanel>
</ListViewItem>
</ListView.Items>
</ListView>
</SplitView.Pane>
<SplitView.Content>
<Frame Name="MainPageMyFrame" />
</SplitView.Content>
</SplitView>
我们走了,它的.cs:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
MainPageListView.SelectedIndex = 0;
}
private void MainPageRelativePanelButton_Click(object sender, RoutedEventArgs e)
{
MainPageSplitView.IsPaneOpen = !MainPageSplitView.IsPaneOpen;
}
private void ListView_ItemClick(object sender, ItemClickEventArgs e)
{
if((sender as ListView).SelectedIndex == 0) // rozklad jazdy
{
MainPageMyFrame.Navigate(typeof(Pages.MainWindow)); // from here i want to navigate
}
}
}
MainWindow.xaml(我要导航的页面)
<UserControl
x:Class="RozkladJazdy.Pages.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:RozkladJazdy.Pages"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<StackPanel Name="MainPageStackPanel" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock VerticalAlignment="Center" Name="MainPageTextBlock"/>
<ProgressRing Name="MainPageProgressRing" IsActive="True" Width="50" Height="50"/>
</StackPanel>
</Grid>
</UserControl>
.cs:
using RozkladJazdy.Model;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236
namespace RozkladJazdy.Pages
{
public sealed partial class MainWindow : UserControl
{
public static string[,] Lines;
public MainWindow()
{
this.InitializeComponent(); // here the error is thrown
}
}
}
就是这样,这里有什么问题吗?
答案 0 :(得分:1)
@Anthony Russell和@SWilko说, 问题是我想导航到UserControl,我应该导航到一个页面。
在我创建WPF应用程序之前,所以我忘记了在UWP中它应该是一个页面。