2

我在这里查看了一些不同的方法,但是在尝试将标签背景颜色从一个类更改为另一个页面时没有成功

我有一个名为 lbConnectionStatus 的 ServerSettings.xaml 标签我也有 Content\ServerSettings.xmal.cs 我有一个名为 SQLConnectionClass.cs 的单独类,它根据成功/失败更改标签背景颜色

这是我的 SQLConnectionClass.cs

class SQLConnectionClass
{
    public void TestConnectivity(String server, String user, String pass, String db)
    {
        string ConnectionString = @"Data Source=" + server +
                       ";Initial Catalog=" + db +
                       ";User ID=" + user +
                       ";Password=" + pass;
        SqlConnection Connection = new SqlConnection(ConnectionString);
        try
        {
            Connection.Open();
            Connection.Close();
            success();
        }
        catch (Exception ex)
        {
           failed(ex.Message.ToString());
        }
    }

    void success()
    {
        MessageBox.Show("Success function","Message");
        Content.ServerSettings mine = new Content.ServerSettings();
        mine.lbConnectionStatus.Background = Brushes.Green;
    }

    void failed(string msg)
    {
        MessageBox.Show(msg,"Message");
        Content.ServerSettings mine = new Content.ServerSettings();
        mine.lbConnectionStatus.Background = Brushes.Red;

    }
}

我试过做以下没有运气

 Content.ServerSettings mine = new Content.ServerSettings();
 mine.lbConnectionStatus.Background = Brushes.Green;

这是 Content\ServerSettings.xmal.cs 的代码

 namespace SQLServer.Content
 {
   /// <summary>
   /// Interaction logic for Server_Settings.xaml
   /// </summary>
   public partial class ServerSettings : UserControl
  {
      public ServerSettings()
    {
        InitializeComponent();
    }

    private void btTest_Click(object sender, RoutedEventArgs e)
    {
        SQLConnectionClass con = new SQLConnectionClass();
        con.TestConnectivity(tbServer.Text, tbUsername.Text, tbPassword.Text, tbDatabase.Text);
    }
}
}

这是实际 ServerSettings.xaml 的代码

<UserControl x:Class="SQLServer.Content.ServerSettings"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>

    <StackPanel>
        <Label Content="Server Address &amp; Instance Name" />
        <TextBox x:Name="tbServer" Text=".\Autobase" Margin="0,10"/>
        <Label Content="Database Name" />
        <TextBox x:Name="tbDatabase" Text="ABSystem7" Margin="0,10"/>
        <Label Content="User-name" />
        <TextBox x:Name="tbUsername" Text="sa" Margin="0,10"/>
        <Label Content="Password" />
        <TextBox x:Name="tbPassword" Text="user" Margin="0,10"/>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="10" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Label Grid.Row="0" Grid.Column="0" Content="Connection Status" Margin="0,10"/>
            <Label x:Name="lbConnectionStatus" Grid.Row="0" Grid.Column="2">
                <Border Margin="0,12" BorderBrush="White" Background="Transparent" CornerRadius="15" BorderThickness="2,2,2,2" Height="15" Width="15" ClipToBounds="True"/>
            </Label>

        </Grid>
        <TextBlock x:Name="tbMessage" Text="TESTING THIS CRAP OUT TO MAKE SURE THAT IT WORKS THE WAY THAT IT IS SUPPOSED TO BUT I AM NOT REALLY SURE WTF I AM DOING BUT HEY I CAN AT LEAST TRY TO MAKE IT WORK CORRECTLY" TextWrapping="Wrap" />
        <Button x:Name="btTest" Content="Test Connection" VerticalAlignment="Bottom" Margin="0,20" Click="btTest_Click"/>

    </StackPanel>

</Grid>
</UserControl>
4

1 回答 1

1

您正在尝试创建 Content.ServerSettings() 的新实例,然后修改其标签背景,这是一个错误。

快速而肮脏的解决方案:

为了实现您的需要,您可以将 ServerSettings 的引用传递给您的 SQLConnectionClass,然后更改其 lbConnectionStatus.Background。 我没有测试这个。

你的 SQL ConnectionClass 看起来像

class SQLConnectionClass
{

    ServerSettings  _serverSettings, 

    public void TestConnectivity(String server, String user, String pass, String db, ServerSettings serverSettings )
    {

       _serverSettings = serverSettings;

        string ConnectionString = @"Data Source=" + server +
                       ";Initial Catalog=" + db +
                       ";User ID=" + user +
                       ";Password=" + pass;
        SqlConnection Connection = new SqlConnection(ConnectionString);
        try
        {
            Connection.Open();
            Connection.Close();
            success();
        }
        catch (Exception ex)
        {
           failed(ex.Message.ToString());
        }
    }

    void success()
    {
        MessageBox.Show("Success function","Message");
        Content.ServerSettings mine = new Content.ServerSettings();
        _serverSettings.lbConnectionStatus.Background = Brushes.Green;
    }

    void failed(string msg)
    {
        MessageBox.Show(msg,"Message");
        Content.ServerSettings mine = new Content.ServerSettings();
        _serverSettings.lbConnectionStatus.Background = Brushes.Red;

    }
}

并且在您的 ServerSettings 类中,您可以将其引用传递给 SQLConnectionClass

公共部分类 ServerSettings : UserControl { public ServerSettings() { InitializeComponent(); }

private void btTest_Click(object sender, RoutedEventArgs e)
{
    SQLConnectionClass con = new SQLConnectionClass();
    con.TestConnectivity(tbServer.Text, tbUsername.Text, tbPassword.Text, tbDatabase.Text, this);
}

}

没有 mvvm 的更好方法

您的 SQLConnectionClass 可以根据连接的成功返回 true 或 false,而 ServerSettings 类可以负责显示消息并修改其标签背景。这种方式 SQLConnectionClass 不与 UI 紧密耦合。这也不是最好的方法,但这可能会让你产生解耦的想法。我也没有测试这个。

你的 SQL ConnectionClass 看起来像

class SQLConnectionClass
    {



    public bool TestConnectivity(String server, String user, String pass, String db,  )
    {

       _serverSettings = serverSettings;

        string ConnectionString = @"Data Source=" + server +
                       ";Initial Catalog=" + db +
                       ";User ID=" + user +
                       ";Password=" + pass;
        SqlConnection Connection = new SqlConnection(ConnectionString);

         Connection.Open();
         Connection.Close();
         return true;
    }      

}

public partial class ServerSettings : UserControl
  {
      public ServerSettings()
    {
        InitializeComponent();
    }
    private void btTest_Click(object sender, RoutedEventArgs e)
    {

try{
        SQLConnectionClass con = new SQLConnectionClass();
        con.TestConnectivity(tbServer.Text, tbUsername.Text, tbPassword.Text, tbDatabase.Text);
         MessageBox.Show("Success function","Message");
         lbConnectionStatus.Background = Brushes.Green;

}
catch(Exception ex)       
{
   MessageBox.Show(ex.Message.ToString(),"Message");
   lbConnectionStatus.Background = Brushes.Red;
}
 }
}
于 2013-09-21T19:42:28.077 回答