我已经在SO上查看了几种不同的方法,但在尝试将标签背景颜色从一个类更改为另一个页面时却没有成功
我有一个名为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 & 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>
答案 0 :(得分: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
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, 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;
}
}
}