如何在Window中添加UserControl并分配x:Name

时间:2018-04-12 12:39:31

标签: c# wpf

首先抱歉我的英语和对不起,我是初学者,也许我尝试做一些不可能的事情。

我的目标是创建一个UserControl,使用不同的参数重用不同的windows / pages / usercontrols中的代码。我开发了一个类来打印盒子来跟踪视频上的对象。

我的问题是编译时我收到了以下错误:

  

名称' mediaBoxes'在当前上下文中不存在

如果我改变:

<local:MediaBoxes x:Name="mediaBoxes"></local:MediaBoxes>

而不是

<local:MediaBoxes Name="mediaBoxes"></local:MediaBoxes>

我收到了以下错误:

  

类型名称MediaBoxes在MyModule

类型中不存在

如果我删除属性Name并且我没有调用InitVideo,所有代码都编译,并且可以执行完美..但我的播放器总是加载,因为我需要初始化实例。

简化我的代码,我有:

InfoPage.xaml

<Window x:Class="MyModule.InfoPage"
    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:local="clr-namespace:MyModule"
    mc:Ignorable="d"
    d:DataContext="{d:DesignInstance  local:InfoPage}">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Content="PlayerBoxes" />
        <DockPanel Grid.Row="1" Name="containerVideo">
            <local:MediaBoxes Name="mediaBoxes"></local:MediaBoxes>
        </DockPanel>
    </Grid>

</Window>

InfoPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using Fulcrum.Model;
using Genetec.Sdk;
using Genetec.Sdk.Workspace;
using MyModule.Helpers;

namespace MyModule
{
    public partial class InfoPage : Window
    {
        #region Constructors
        public InfoPage(SearchPage mainTaskUc, ODetection det)
        {
            InitializeComponent();
            _detection = det;
            mainTask = mainTaskUc;

            DataContext = this;
        }

        #endregion

        #region Public Methods

        public void Initialize(Workspace workspace)
        {
            if (workspace == null)
                throw new ArgumentNullException("workspace");

            Workspace = workspace;
            _sdkEngine = Workspace.Sdk;

            Show();

            mediaBoxes.InitVideo(mainTask.SearchSelectedClips[0], _detection, _sdkEngine);
        }
        #endregion
    }

mediaBoxes是一个用户控件:

MediaBoxes.xaml

<UserControl x:Class="MyModule.MediaBoxes"
         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:local="clr-namespace:MyModule"
         xmlns:media="clr-namespace:Genetec.Sdk.Media;assembly=Genetec.Sdk.Media"
         mc:Ignorable="d"
         d:DesignHeight="300" d:DesignWidth="300">

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../Styles/Colors.xaml"></ResourceDictionary>
            <ResourceDictionary Source="../Styles/Fonts.xaml"></ResourceDictionary>
            <ResourceDictionary Source="../Styles/Texts.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

<Grid>

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

    <Grid Grid.Row="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <DockPanel Name="containerVideo" Grid.Row="0">
            <Canvas Name="canvas" Background="{StaticResource GreyDark4Brush}"
                    Width="{Binding ActualWidth, ElementName=mediaPlayer}"
                    Height="{Binding ActualHeight, ElementName=mediaPlayer}"
                    Visibility="Collapsed">
                <media:MediaPlayer Name="mediaPlayer"
                                   Width="{Binding ActualWidth, ElementName=containerVideo}"
                                   Height="{Binding ActualHeight, ElementName=containerVideo}" />

                <Canvas Name="CanvasBoxes" />

                <Canvas Name="CanvasControls">
                    <TextBlock Name="plyrBoxesCameraTitle" Canvas.Top="24" Canvas.Left="24" Foreground="LightGray" />
                    <TextBlock Name="plyrBoxesDateTime" Canvas.Top="56" Canvas.Left="24" Foreground="LightGray" />
                    <Button Name="plyrBoxesBookmarkBtn" Canvas.Top="24" Canvas.Right="24">
                        <Button.Content>
                            <TextBlock FontFamily="{StaticResource MaterialIcons}" Text="&#xE867;"
                                       Foreground="LightGray" />
                        </Button.Content>
                    </Button>
                    <Grid Canvas.Bottom="0" Canvas.Left="0"
                          Width="{Binding ActualWidth, ElementName=CanvasControls}">
                        <Grid.Resources>
                            <Style TargetType="Border">
                                <Setter Property="Padding" Value="24" />
                            </Style>
                        </Grid.Resources>

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Border Grid.Column="0" HorizontalAlignment="Left">
                            <DockPanel>
                                <Button Name="plyrBoxesVolumeOnBtn">
                                    <Button.Content>
                                        <TextBlock FontFamily="{StaticResource MaterialIcons}" Text="&#xE050;"
                                                   Foreground="LightGray" />
                                    </Button.Content>
                                </Button>
                                <Button Name="plyrBoxesVolumeOffBtn" Visibility="Collapsed">
                                    <Button.Content>
                                        <TextBlock FontFamily="{StaticResource MaterialIcons}" Text="&#xE04F;"
                                                   Foreground="LightGray" />
                                    </Button.Content>
                                </Button>
                            </DockPanel>
                        </Border>
                        <Border Grid.Column="1" HorizontalAlignment="Center">
                            <DockPanel>
                                <Button Name="plyrBoxesPlayBtn" Visibility="Collapsed">
                                    <Button.Content>
                                        <TextBlock FontFamily="{StaticResource MaterialIcons}" Text="&#xE037;"
                                                   Foreground="LightGray" />
                                    </Button.Content>
                                </Button>
                                <Button Name="plyrBoxesPauseBtn">
                                    <Button.Content>
                                        <TextBlock FontFamily="{StaticResource MaterialIcons}" Text="&#xE034;"
                                                   Foreground="LightGray" />
                                    </Button.Content>
                                </Button>
                                <Button Name="plyrBoxesRestartSequenceBtn">
                                    <Button.Content>
                                        <TextBlock FontFamily="{StaticResource MaterialIcons}" Text="&#xE5D5;"
                                                   Foreground="LightGray" />
                                    </Button.Content>
                                </Button>
                            </DockPanel>
                        </Border>
                        <Border Grid.Column="2" HorizontalAlignment="Right">
                            <TextBlock Name="plyrBoxesCurrentTime" Foreground="LightGray" />
                        </Border>
                    </Grid>
                </Canvas>

            </Canvas>

            <StackPanel Name="loadingVideoPlayer" Visibility="Visible"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center">
                <TextBlock Style="{StaticResource LoadingSpinningText}" FontSize="52" Width="52" Height="52" />
                <TextBlock FontSize="18" Margin="0 16">Loading Video</TextBlock>
            </StackPanel>
        </DockPanel>
    </Grid>

</Grid>

和MediaBoxes.xaml.cs

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Threading;
using Genetec.Sdk;

namespace MyModule
{
public partial class MediaBoxes : UserControl
{
    public MediaBoxes()
    {
        InitializeComponent();
    }

    private IEngine _sdkEngine;
    private SearchSelectedClipModel _selectedCamera;
    private ODetection _detection;

    public void InitVideo(SearchSelectedClipModel selectedCamera, ODetection detection, IEngine sdkEngine)
    {
        _sdkEngine = sdkEngine;
        _selectedCamera = selectedCamera;
        _detection = detection;

        ...
}

非常感谢!!!

更新

好的,我发现了真正的问题..我的代码没有任何问题......但我在根路径中定义了一个与我的命名空间名称相同的类。 -.-&#39;

谢谢大家的帮助!!

2 个答案:

答案 0 :(得分:0)

有些ui的东西可以有名字,有些可以。您不需要为控件命名。

您的usercontrol是一个frameworkelement,因此它将具有标准事件。 包括初始化和加载。 您可以通过XAML将其中任何一个与事件处理程序挂钩。

 <local:MediaBoxes Initialized="MediaBoxes_Initialized"/>

如果你输入initialized =那么你可以接受它给你的存根事件处理程序。

答案 1 :(得分:-1)

您需要按照以下

命名
<local:MediaBoxes x:Name="mediaBoxes"></local:MediaBoxes>

因为你的usercontrol'MediaBoxes'和'Window'在同一名称空间(MyModule)中,所以如果你要写下面的

<local:MediaBoxes Name="mediaBoxes"></local:MediaBoxes>

您将收到以下错误

Because 'Microsoft.VisualStudio.DesignTools.Xaml.LanguageService.Semantics.Metadata.ReflectionTypeNode' is implemented in the same assembly, you must set the x:Name attribute rather than the Microsoft.VisualStudio.DesignTools.Xaml.LanguageService.Semantics.Metadata.ReflectionPropertyNode attribute. 

所以以下列方式给你usercontrol命名

<local:MediaBoxes x:Name="mediaBoxes"></local:MediaBoxes>

并在代码后面使用以下

访问它
mediaBoxes.InitVideo(mainTask.SearchSelectedClips[0], _detection, _sdkEngine);