qq_20634189 2022-12-24 15:20 采纳率: 100%
浏览 25
已结题

Prism框架登录窗口跳转到主窗口后 ToggleButton 无法找到资源问题

C# Prism框架登录窗口跳转到主窗口后 ToggleButton 无法找到绑定元素的问题,
直接跳转到主窗口正常

<hc:GlowWindow xmlns:Views="clr-namespace:WPFjjhfuzhu41.Views"
x:Class="WPFjjhfuzhu41.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation%22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml%22
xmlns:prism="http://prismlibrary.com/%22
xmlns:hc="https://handyorg.github.io/handycontrol%22
ResizeMode="CanResizeWithGrip"
Style="{StaticResource WindowGlow}"
ActiveGlowColor="{DynamicResource PrimaryColor}"
Background="{DynamicResource MainContentBackgroundBrush}"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors%22
xmlns:ext="clr-namespace:WPFjjhfuzhu41.Extensions"
Height="768"
Width="1280">

<hc:GlowWindow.NonClientAreaContent>
    <StackPanel Orientation="Horizontal">
        <ToggleButton Panel.ZIndex="1"
                      Margin="-10 0 0 0"
                      Style="{StaticResource ToggleButtonIcon}"
                      Background="DarkViolet"
                      hc:IconSwitchElement.Geometry="{StaticResource RightGeometry}"
                      hc:IconSwitchElement.GeometrySelected="{StaticResource LeftGeometry}"
                    IsChecked="{Binding IsOpen ,ElementName=DrawerLeft}"/>

        <TextBlock Text="{Binding Title}"
                   FontSize="14"
                   VerticalAlignment="Center"></TextBlock>

    </StackPanel>
</hc:GlowWindow.NonClientAreaContent>


<Grid>

    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="30" />

    </Grid.RowDefinitions>


    <hc:Drawer Name="DrawerLeft"
               Dock="Left"
               ShowMode="Push">


        <Border Background="{DynamicResource RegionBrush}"
                Width="300"
                BorderThickness="0,1,0,0"
                BorderBrush="{DynamicResource BorderBrush}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                </Grid.RowDefinitions>




                <hc:SideMenu Name="sideMenu"
                             Grid.Row="1">

                    <hc:SideMenuItem Header="配置"
                                     Command="{Binding NavigateCommand}"
                                     CommandParameter="{Binding Header,RelativeSource={RelativeSource Self}}">
                        <hc:SideMenuItem.Icon>
                            <TextBlock Text="&#xe600;"
                                       Style="{StaticResource TextBlockSubTitle}" />
                        </hc:SideMenuItem.Icon>
                    </hc:SideMenuItem>

                    <hc:SideMenuItem Header="关于"
                                     Command="{Binding NavigateCommand}"
                                     CommandParameter="{Binding Header,RelativeSource={RelativeSource Self}}">

                        <hc:SideMenuItem.Icon>
                            <TextBlock Text="&#xe600;"
                                       Style="{StaticResource TextBlockSubTitle}" />
                        </hc:SideMenuItem.Icon>
                    </hc:SideMenuItem>
                </hc:SideMenu>

            </Grid>
        </Border>
    </hc:Drawer>


    <ContentControl Grid.Row="0"
                    prism:RegionManager.RegionName="{x:Static ext:PrismManager.MainViewRegionName}" />

    <StackPanel Grid.Row="1"
                Orientation="Horizontal"
                VerticalAlignment="Center"
                Margin="10 0 0 0">


        <StackPanel Orientation="Horizontal"
                    Width="150"
                    Margin="0 0 20 0">
            <TextBlock Text="总金币:" />
            <TextBlock Text="{Binding TotalCoin,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"></TextBlock>
        </StackPanel>


        <StackPanel Orientation="Horizontal"
                    Width="150"
                    Margin="0 0 20 0">
            <TextBlock Text="最低金币:" />
            <TextBlock Text="{Binding HighCoin,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"></TextBlock>
        </StackPanel>



        <StackPanel Orientation="Horizontal"
                    Width="150"
                    Margin="0 0 20 0">

            <TextBlock Text="最低金币:" />
            <TextBlock Text="{Binding LowCoin,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"></TextBlock>
        </StackPanel>



    </StackPanel>




</Grid>

运行结果及详细报错内容

错误 1 null IsOpen ToggleButton.IsChecked Nullable`1 找不到源: ElementName=DrawerLeft。 \views\mainwindow.xaml 22

我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”

付费悬赏

  • 写回答

1条回答 默认 最新

  • |__WhoAmI__| 2022-12-24 19:02
    关注

    当 ToggleButton 元素绑定到的数据在主窗口的 ViewModel 中时,它们在窗口加载时可能尚未被创建。

    要解决这个问题,可以在主窗口的 ViewModel 中使用延迟加载,以便在需要绑定的数据可用时才进行绑定。例如可以使用 Lazy 类型来延迟初始化绑定的属性:

    private Lazy<bool> isOpen = new Lazy<bool>(() => false);
    
    public bool IsOpen
    {
        get { return isOpen.Value; }
        set { SetProperty(ref isOpen, value); }
    }
    

    这样,当 ToggleButton 元素尝试绑定到 IsOpen 属性时,这个属性才会被初始化。
    还可以使用 Prism 的导航机制来在登录窗口和主窗口之间进行跳转。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月1日
  • 已采纳回答 12月24日
  • 修改了问题 12月24日
  • 创建了问题 12月24日

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用