gpt4 book ai didi

c# - 从 WPF 应用程序中的代码访问 UserControl

转载 作者:行者123 更新时间:2023-11-30 20:52:46 25 4
gpt4 key购买 nike

我在我的 wpf 应用程序中创建了一个用户控件。我是这样使用它的:

<Page x:Class="InstallerToolkit.Pages.PageVideosNvr"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:my="clr-namespace:MyProject.UserControls"
mc:Ignorable="d"
d:DesignHeight="525" d:DesignWidth="1050"
Title="PageVideos">
<Grid>
<my:UserControlVideoPlayer Name="VideoPlayer" Margin="559,74,35,155">

</my:UserControlVideoPlayer>
</Grid>

现在在我的 C# 页面中,我想访问它,但是当我在 C# 页面的代码中键入它的名称时,VideoPlayer 对象没有出现。

我要做什么才能访问它,因为我想设置它的属性之一。

最佳答案

x:Name 给您的 UserControl 而不是 Name

<my:UserControlVideoPlayer x:Name="VideoPlayer" Margin="559,74,35,155">

现在可以使用 this.VideoPlayer 在代码中访问它。

我建议将它作为一个经验法则,每当引用 XAML 中的元素时始终使用 x:Name

引用this Name 和 x:Name 的区别。

关于c# - 从 WPF 应用程序中的代码访问 UserControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20593808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com