C# 写的小玩意,遇到一个问题。
登录时缺少一个加载框 然后我就找了个代码 整上去了。
结果:点击登录执行方法 然后逻辑执行完了 加载框才显示。
无论我将加载框的代码写到哪。只有将登录逻辑删掉 才能显示。就很离谱
我原以为是因为线程之类的,结果开了线程就报错,因为主占用 。然后我一想,不应该这么麻烦呀。
#这是原由
后面的异常不是的 后面的异常是结束完之后跳转到另一个窗体 之后的的了
就是点击登录直接显示加载框 然后执行登录逻辑。
而现实却是点击之后执行逻辑 之后执行完了才会有加载框
public LoginPage()
{
WindowStartupLocation = WindowStartupLocation.CenterScreen;
InitializeComponent();
this.logding.Visibility = Visibility.Collapsed;
busy = new BusyDecorator(this.canvas_bu);
}
BusyDecorator busy;
bool isstart = false;
//找个方法套起来执行
//显示时执行
this.logding.Visibility = Visibility.Visible;
busy.StartDecorator();
isstart = true;
//BusyDecorator类
internal class BusyDecorator
{
///
/// 条的数量
///
int _elementCount;
///
/// 圆的半径
///
double _radious = 10;
///
/// 执行动画的DispatcherTimer
///
DispatcherTimer _animationTimer;
///
/// 当前条的索引位置
///
int _currentElementIndex = 0;
///
/// 需要变换的透明度个数
///
int _opacityCount;
///
/// 透明度间的间隔
///
double _opacityInterval;
///
/// 透明度
///
double _opacity;
///
/// 最小透明度
///
double _minOpacity;
///
/// 条的数组
///
object[] _elements;
///
/// 画布
///
private Canvas _canvas;
private void CreateElements(Canvas canvas, double Left, double Top)
{
_elementCount = 12;
_opacity = 1;
_minOpacity = 0.3;
double surplusOpacity = _opacity - _minOpacity;
_opacityCount = (int)(_elementCount * 0.5);
_opacityInterval = surplusOpacity / _opacityCount;
_elements = new object[_elementCount];
for (int i = 0; i < _elementCount; i++)
{
Rectangle rect = new Rectangle();
rect.Fill = new SolidColorBrush(Colors.Black);
rect.Width = 5;
rect.Height = 5;
rect.RadiusX = 2;
rect.RadiusY = 2;
if (i < _opacityCount)
{
rect.Opacity = _opacity - i * _opacityInterval;
}
else
{
rect.Opacity = _minOpacity;
}
rect.SetValue(Canvas.LeftProperty, Left + _radious * Math.Cos(360 / _elementCount * i * Math.PI / 180));
rect.SetValue(Canvas.TopProperty, Top - 2.5 - _radious * Math.Sin(360 / _elementCount * i * Math.PI / 180));
rect.RenderTransform = new RotateTransform(360 - 360 / _elementCount * i, 0, 2.5);
canvas.Children.Add(rect);
_elements[i] = rect;
}
_currentElementIndex = 0;
}
private void _animationTimer_Tick(object sender, EventArgs e)
{
try
{
_currentElementIndex--;
_currentElementIndex = _currentElementIndex < 0 ? _elements.Length - 1 : _currentElementIndex;
int opacitiedCount = 0;
for (int i = _currentElementIndex; i < _currentElementIndex + _elementCount; i++)
{
int j = i > _elements.Length - 1 ? i - _elements.Length : i;
if (opacitiedCount < _opacityCount)
{
((Rectangle)_elements[j]).Opacity = _opacity - opacitiedCount * _opacityInterval;
opacitiedCount++;
}
else
{
((Rectangle)_elements[j]).Opacity = _minOpacity;
}
}
}
catch (Exception ex)
{ }
}
public BusyDecorator(Canvas canvas)
{
this._canvas = canvas;
_animationTimer = new DispatcherTimer();
_animationTimer.Interval = TimeSpan.FromMilliseconds(40);
_animationTimer.Tick += new EventHandler(_animationTimer_Tick);
CreateElements(canvas, canvas.Width / 2, canvas.Height / 2);
}
public void StartDecorator()
{
_canvas.Visibility = Visibility.Visible;
_animationTimer.Start();
}
public void StopDecorator()
{
_canvas.Visibility = Visibility.Hidden;
_animationTimer.Stop();
}
}
//XAML
<Grid Name="logding" DockPanel.Dock="Left" Grid.ColumnSpan="4" Margin="0,0,0,10" Grid.RowSpan="7" Background="White">
<Canvas x:Name="canvas_bu" Width="200" Height="200" VerticalAlignment="Top" Opacity="1" Background="White" Margin="128,33,156,0"/>
</Grid>
下面是登录的地方
public void LoginUser(object sender, RoutedEventArgs e)
{
logingg(true);
Trace.WriteLine("执行加载旋转");
Login();
Trace.WriteLine("执行登录逻辑处理");
}
public void logingg(bool off_no)
{
if (off_no == true)
{
//显示
this.logding.Visibility = Visibility.Visible;
busy.StartDecorator();
isstart = true;
}
else
{
//隐藏
this.logding.Visibility = Visibility.Collapsed;
busy.StartDecorator();
isstart = false;
}
}
public void Login() {
if (UserName.Text != null && !UserName.Text.Equals(""))
{
if (Pwd.Text != null && !Pwd.Text.Equals(""))
{
string UserCount = "select COUNT(*) from User where UserName= '" + UserName.Text + "' and " + "Password= '" + Pwd.Text + "'";
int obj = Convert.ToInt32(MsqlTo.Count(UserCount));
if (obj == 0)
{
logingg(false);
// MessageBox.Show("您的账号或者密码错误", "提示");
Trace.WriteLine("主线程结束---------------------------您的账号或者密码错误---------------------------------------------------------------------------------");
}
else if (obj == -1)
{
logingg(false);
// MessageBox.Show("数据库未连接,请检查网络。或联系作者", "提示");
Trace.WriteLine("主线程结束------------数据库未连接,请检查网络------------------------------------------------------------------------------------------------");
}
else
{
//登录成功后
//获取用户的设备的编码
//----查询用户id
Object userId = MsqlTo.UserAllSum(UserName.Text);
//----通过用户ID查询服务器是否存在当前设备编号
//生成设备编码 进去查询
uid = (int)userId;
Object ioi = MsqlTo.SelectEquipment(userId, Tims.GetCodes());
if (ioi == null)
{
MessageBox.Show("账户中不存在该设备,请激活", "提示");
ActivationPage reg = new ActivationPage();
reg.Show(); //打开新窗口
this.Close(); //关闭当前窗口
}
else
{
object emID = MsqlTo.SelectEquipmentID(Tims.GetCodes());
object AcodeTime = MsqlTo.SelectYtime(emID);
object UsageTime = MsqlTo.SelectUsageTime(emID);
DateTime dateTime = (DateTime)UsageTime;
var nextTime = dateTime.AddMonths(Convert.ToInt32(AcodeTime));
MessageBox.Show("登陆成功,剩余时间:" + nextTime, "提示");
PrintPage reg = new PrintPage();
reg.Show(); //打开新窗口
this.Close(); //关闭当前窗口
}
}
}
else
{
MessageBox.Show("请输入账号或者密码", "警告");
}
}
else
{
MessageBox.Show("请输入账号或者密码", "警告");
}
}