简体   繁体   English

将重点放在密码箱上

[英]Setting focus on Passwordbox

so I have an C# WPF Application with an Login Window. 所以我有一个带有登录窗口的C#WPF应用程序。 In this Window I also have a Checkbox, which safes the Username for the next times he starts this Application (Its like a "Remember me"). 在此窗口中,我还有一个复选框,可确保用户下次启动该应用程序时的用户名安全(就像“记住我”一样)。

The normal Focus for this Window is obviousely the Username. 显然,此窗口的正常“焦点”是用户名。 But If the User selected the Remember me Checkbox, the passwordbox should be in Focus (because Username is already filled). 但是,如果用户选择了“记住我”复选框,则密码框应位于“焦点”位置(因为用户名已填写)。

So I Wrote this Code: 所以我写了这段代码:

in Xaml: 在Xaml中:

<PasswordBox
    Name="PW"
    Grid.Row="2"
    Grid.Column="1"
    Margin="10 0 10 0"
    VerticalContentAlignment="Center"
    Height="45"/>

<CheckBox
    Name="CB"
    Grid.Row="1"
    Grid.Column="1"
    VerticalAlignment="Bottom"
    Margin="10"
    Content="Remember me"/>

<TextBox
    Name="Tbx"
    Grid.Row="1"
    Grid.Column="1"
    Margin="10 0 10 0"
    Text="{Binding Path=User}"
    VerticalContentAlignment="Center"
    Height="45"/>

<Button
    Name="Btn"
    Grid.Row="3"
    Grid.Column="1"
    Content="Login"
    Height="40"
    Width="100"
    VerticalAlignment="Center"
    HorizontalAlignment="Center"
    Command="{Binding Path=LoginUser}"
    Click="Btn_Click" 
    IsDefault="True"/>

The Binding in the Textbox is just a Property, which gets the entered Value. 文本框中的绑定只是一个属性,它获取输入的值。

The Binding in the Button is also just a Property, which checks for Username and Password, and then opens another Window (So I dont think, thats nessecary to show). 按钮中的绑定也只是一个属性,该属性检查用户名和密码,然后打开另一个窗口(所以我不认为这很必要显示)。

In xaml.cs 在xaml.cs中

private void Btn_Click(object sender, RoutedEventArgs e)
    {
        if (Cb.IsChecked == true)
        {
        Properties.Settings.Default.Username = Tbx.Text;
        }
    }

So with this Button_Click I safe the entered Username, so the Application can Remember it for the next time. 因此,使用此Button_Click,我可以保护输入的用户名的安全,以便应用程序下次可以记住它。 This works as it should. 这可以正常工作。

And now the interesting part, the Window_Loaded event: 现在有趣的部分是Window_Loaded事件:

try
    {
    IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForAssembly();
    StreamReader srReader = new StreamReader(new IsolatedStorageFileStream("isotest", FileMode.OpenOrCreate, isolatedStorage));

    string Input = srReader.ReadLine().TrimStart();
    string InputCut = Input.Substring(0, Input.Length - 25);
    Tbx.Text = InputCut;
    Cb.IsChecked = true;
    Pb.Focus()
    }
    catch(Exception ex)
    {}

So If The CheckBox is Checked, he will go through the try part, and is going for Pb.Focus() (I´ve stepped it through), but somehow the passwordbox wont get the focus. 因此,如果CheckBox被选中,他将遍历try部分,并准备使用Pb.Focus()(我已逐步完成),但密码框却无法获得焦点。 I then thought, it may be because of the Try - Catch, so I tried to set the Focus outside of that, but the same problem occours. 然后我想,可能是因为“尝试-抓住”,所以我试图将“焦点”设置在此范围之外,但同样会出现问题。 The Passwortbox wont get Focused. Passwortbox不会变得专注。

I have already searched Google for that exact problem, but I couldnt find anything (Only normal Questions on how to set the Focus on a Passwordbox) 我已经在Google上搜索了确切的问题,但找不到任何内容(只有关于如何将焦点设置在密码箱上的常见问题)

Also I have checked here on stackoverflow, but the only thing I found was this: Set focus on PasswordBox when application starts And this obviousely isnt my problem at all, I already have the Focus In The Window_Loaded. 我也在这里检查了stackoverflow,但是我发现的唯一发现是: 在应用程序启动时将焦点放在PasswordBox上,这显然不是我的问题,我已经在Window_Loaded中拥有了Focus。

The expected outcome is, that the passwordbox is getting the Focus, but somehow it wont. 预期的结果是,密码框正在获得Focus,但是某种程度上却不会。 I am not even getting any Error Messages, so I cant provide any. 我什至没有收到任何错误消息,因此我无法提供任何错误消息。

If you need any more Information just hit me up, I will edit the Question then. 如果您需要更多信息,请直接打给我,然后我将编辑问题。 I hope anyone of you knows this Problem, and can help me here. 我希望任何人都知道这个问题,并可以在这里为我提供帮助。 Thanks! 谢谢!

I guess your problem might be hidden by your empty catch. 我想您的问题可能被您的空白所掩盖。

IE you have an error but that catch just "eats" it so you never see it. IE浏览器您有一个错误,但捕获只是“吃”它,所以您永远不会看到它。

It seems odd that you can step through and not notice it jump to the catch though. 您可以单步执行,却没有注意到它跳到了陷阱,这似乎很奇怪。

Of course stepping through will mess up any focussing. 当然,逐步执行将使您的工作变得混乱。

Here is working markup and code: 这是工作标记和代码:

    <StackPanel>
        <TextBox
            Name="Tbx" Height="45"/>
        <PasswordBox
            Name="PW" Height="45"/>
        <CheckBox
           Name="CB" Content="Remember me"/>
        <Button
          Name="Btn"  Height="40"  Width="100" IsDefault="True"/>
    </StackPanel>


    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        string InputCut = "Blaa";
        Tbx.Text = InputCut;
        CB.IsChecked = true;
        PW.Focus();
    }

Create a new solution. 创建一个新的解决方案。

NOT WHAT YOU HAVE NOW. 现在还没有。

Because whatever is wrong might well lie somewhere in code you've not shown us. 因为任何错误都可能位于代码中的某个地方,所以您没有向我们展示。

Paste the above markup and code into the mainwindow. 将上面的标记和代码粘贴到主窗口中。

Make sure you hook up the loaded event. 确保您挂接了已加载的事件。

Watch it work. 观看它的工作。

My advice would be: 我的建议是:

Remove your try catch and never use an empty catch block again. 删除尝试捕获,不要再使用空的捕获块。

Fix your code reading that string from isolated storage so it's more robust. 修复您的代码,从隔离存储中读取该字符串,使其更健壮。

For wpf it's more usual to use 对于WPF,它通常更常用

 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 

Which is the user's appdata roaming root. 用户的appdata漫游根目录。

If your code still isn't working or erroring then add pieces of your app to the new solution you created until you reproduce your issue in that. 如果您的代码仍然无法正常工作或出错,则将您的应用程序片段添加到您创建的新解决方案中,直到您重现该问题为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM