C#新类文件现在无法获取按钮。

时间:2017-01-27 05:08:28

标签: c# xaml

我正在为我的程序创建一个新的类文件所以我将一些代码移到了这个新的

namespace App1
{
    class BL_PageContent
    {
        public static string VarOutput { get; set; }

        async public void btnCourse1_Click(object sender, RoutedEventArgs e)
        {
            string[] names = new string[3] { "COP3488C,", "UWP1,", "This course is mobile app development." };
            await WorkerAsync(names);
        }


        async private void btnCourse2_Click(object sender, RoutedEventArgs e)
        {
            string[] names = new string[3] { "DOP3488B,", "UWC1,", "This course is Cloud Computing." };
            await WorkerAsync(names);
        }

        async private void btnCourse3_Click(object sender, RoutedEventArgs e)
        {

            string[] names = new string[3] { "BOP3589,", "UWP2,", "This course Computer Programming Java 1." };
            await WorkerAsync(names);

        }

        private async Task WorkerAsync(string[] names)
        {
            string VarOutput = "";

            for (int i = 0; i < names.Length; i++)
            {
                VarOutput = VarOutput + names[i] + "  ";
            }


        }
    }
}

这是该应用的主要类文件。

    public sealed partial class MainPage : Page
{




    public MainPage()
    {

        this.InitializeComponent();
        txtBoxRas.Text = "Rasmussen College";
        txtBoxRas.FontSize = 14;

        txtBoxRas.Foreground = new SolidColorBrush(Windows.UI.Colors.Red);

    }
    private void btnSubmit_Click(object sender, RoutedEventArgs e)
    {
        ProcessCalc();
    }

    private void ProcessCalc()
    {
        Int32 Var1 = Convert.ToInt32(txtBoxInput1.Text) + Convert.ToInt32(txtBoxInput2.Text);
        txtBoxDisplay.Text = Convert.ToString(Var1);
    }


    private async Task WorkerAsync(string[] names)
    {

        txtBoxCourse.Text = BL_PageContent.VarOutput;
        var dialog = new MessageDialog(BL_PageContent.VarOutput);
        await dialog.ShowAsync();


    }



}

}

这是我展示的xaml。这就是错误即将到来我知道它必须链接到这个文件的问题是什么?严重性代码描述项目文件行抑制状态错误CS1061'MainPage'不包含'btnCourse1_Click'的定义,并且没有扩展方法'btnCourse1_Click'接受类型'MainPage'的第一个参数可以找到(你是否缺少using指令或装配参考?)

Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" RenderTransformOrigin="0.643,0.234">
    <TextBlock x:Name="txtBoxRas" HorizontalAlignment="Left" Margin="348,95,0,0" TextWrapping="Wrap" Text="Rasmussen logo" VerticalAlignment="Top" Height="42" Width="230" RenderTransformOrigin="-0.358,1.105"/>
    <TextBlock x:Name="txtBoxDisplay" HorizontalAlignment="Left" Margin="470,195,0,0" TextWrapping="Wrap" Text="Result" VerticalAlignment="Top" Height="57" Width="191"/>
    <TextBox x:Name="txtBoxInput1" HorizontalAlignment="Left" Margin="348,142,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="48" Width="140"/>
    <TextBox x:Name="txtBoxInput2" HorizontalAlignment="Left" Margin="510,142,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="48" Width="151"/>
    <Button x:Name="btnSubmit" Content="add it up!" HorizontalAlignment="Left" Margin="393,252,0,0" VerticalAlignment="Top" Width="268" Click="btnSubmit_Click"/>
    <HyperlinkButton x:Uid="hlkWebSite" Content="Go" HorizontalAlignment="Left" Margin="185,268,0,0" VerticalAlignment="Top"/>

    <Button x:Name="btnCourse1" Content="Course 1" HorizontalAlignment="Left" Margin="41,337,0,0" VerticalAlignment="Top" Click="btnCourse1_Click"/>

    <Button x:Name="btnCourse2" Content="Course 2" HorizontalAlignment="Left" Margin="41,374,0,0" VerticalAlignment="Top" Click="btnCourse2_Click"/>

    <Button x:Name="btnCourse3" Content="Course 3" HorizontalAlignment="Left" Margin="41,411,0,0" VerticalAlignment="Top" Click="btnCourse3_Click"/>



    <TextBox x:Name="txtBoxCourse" HorizontalAlignment="Left" Margin="185,337,0,0" TextWrapping="Wrap" Text="Course Info" VerticalAlignment="Top" Height="106" Width="303"/>
</Grid>

1 个答案:

答案 0 :(得分:0)

你做不到。在XAML中查看此代码:

Page
x:Class="App1.MainPage"

您的XAML包含有关在何处查找视图后面代码的信息。如上所述,它是App1.MainPage。所以你XAML正在MainPage中寻找这些:

<Button x:Name="btnCourse1" Content="Course 1" HorizontalAlignment="Left" Margin="41,337,0,0" VerticalAlignment="Top" Click="btnCourse1_Click"/>

<Button x:Name="btnCourse2" Content="Course 2" HorizontalAlignment="Left" Margin="41,374,0,0" VerticalAlignment="Top" Click="btnCourse2_Click"/>

// ... so on

它不再存在因为你移动了它。所以它在抱怨。没有理由你应该这样做。如果这是尝试重构或移动代码以实现可重用性,那么这不是实现它的方法。

如果要重用代码,请创建可重用的类,然后将它们传递给viewmodel。将您的视图绑定到您的viewmodel。