Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单击Silverlight用户控件中的事件_C#_Silverlight - Fatal编程技术网

C# 单击Silverlight用户控件中的事件

C# 单击Silverlight用户控件中的事件,c#,silverlight,C#,Silverlight,我在Silverlight中有一个用户控件,它有一个按钮单击事件。当我将该用户控件添加到页面时,不会触发click事件。我还有别的事要做吗 更新 用户控件本身触发单击事件。在页面内部时,它不会 页面 用户控制 <UserControl x:Class="RadControlsSilverlightApp2.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http

我在Silverlight中有一个用户控件,它有一个按钮单击事件。当我将该用户控件添加到页面时,不会触发click事件。我还有别的事要做吗

更新 用户控件本身触发单击事件。在页面内部时,它不会 页面


用户控制

<UserControl x:Class="RadControlsSilverlightApp2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" BorderThickness="1"  xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">


您是否在主页中处理的usercontrol中引发了事件

用户控制XAML

这允许在单击用户控件上的按钮时更新主页上的文本块。它引发在主页上处理的事件。EventArgs在用户控件和主页面之间传输信息


希望这有帮助。

您能提供更多详细信息(代码和/或Xaml)。描述没有说明用户控件如何实现按钮单击事件。感谢您的回复。但是我不希望在主页和控件之间进行通信。用户控件是自包含的。您能更具体地说明按钮的作用吗?您是否在用户控件xaml和代码隐藏中添加了单击事件?见上文代码段1和2。用户控件中的单击事件不必引发事件。主页是否与用户控件的内容有关?是需要关闭的弹出窗口吗?你能分享的更多信息都会有帮助。我看你做了一些电话咨询;用户控件是否基于其中一个容器?Jason,我刚刚创建了一个非常简单的用户控件,它在触发事件时没有问题。我必须调查另一个用户控件没有启动的原因。谢谢你的帮助。我给你打了个电话,因为这很有帮助
<UserControl x:Class="RadControlsSilverlightApp2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" BorderThickness="1"  xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
<Grid x:Name="LayoutRoot" Background="White" Height="133">
    <sdk:Label Height="18" HorizontalAlignment="Left" Margin="12,31,0,0" Name="label1" VerticalAlignment="Top" Width="58" Content="Name"  />
    <sdk:Label Content="Password" Height="18" HorizontalAlignment="Left" Margin="12,60,0,0" Name="label2" VerticalAlignment="Top" Width="58" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="89,27,0,0" Name="textBox1" VerticalAlignment="Top" Width="253" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="89,55,0,0" Name="textBox2" VerticalAlignment="Top" Width="253" />
    <Button Content="Login" Height="23" HorizontalAlignment="Left" Margin="161,101,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
public class userNameEventArgs : EventArgs
{
    public string userName{get;set;}
}

public partial class LoginBox : UserControl
{
    public event EventHandler<userNameEventArgs> LoggedIn;

    public LoginBox()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        LoggedIn(this, new userNameEventArgs(){userName=textBox1.Text});
    }
}
<Grid x:Name="LayoutRoot" Background="White">
    <Button Content="Main Page" Height="23" HorizontalAlignment="Left" Margin="36,50,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <local:LoginBox Margin="100,122,179,223" LoggedIn="LoginBox_LoggedIn" />
    <TextBlock Height="23" HorizontalAlignment="Left" Margin="36,376,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" Width="94" />
</Grid>
public partial class MainPage : UserControl
{
    public MainPage()
    {
        // Required to initialize variables
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Mainpage pressed!!");
    }

    private void LoginBox_LoggedIn(object sender, userNameEventArgs e)
    {
        textBlock1.Text = e.userName;
    }
}