我正在使用C#进行WPF应用程序。我想弹出一个对话框,提示用户输入他/她的名字。之后,我将跟踪名称并使用名称将一些数据保存到txt文件中。
例如,
名称输入是 name =“John”
所以我有一个数据 data =“1,2,3”;
然后我将“数据”保存在John.txt文件中。
任何人都知道该怎么做? 我认为问题是如何弹出一个对话框供用户输入名称。
谢谢!
答案 0 :(得分:33)
我更喜欢使用不会锁定应用程序的对话框,并远离更传统的Win32对话框。
示例强>
隐藏输入对话
在此示例中,我使用了我正在为我的应用程序使用的基于MVVM的解决方案的简化版本。它可能不是很漂亮,但应该给你一个关于它背后的基础知识的坚实想法。
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<Button Content="Cool Button" x:Name="CoolButton" Click="CoolButton_Click"/>
<ListBox x:Name="MyListBox"/>
</StackPanel>
<!-- It's important that this is in the end of the XAML as it needs to be on top of everything else! -->
<Grid x:Name="InputBox" Visibility="Collapsed">
<Grid Background="Black" Opacity="0.5"/>
<Border
MinWidth="250"
Background="Orange"
BorderBrush="Black"
BorderThickness="1"
CornerRadius="0,55,0,55"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<StackPanel>
<TextBlock Margin="5" Text="Input Box:" FontWeight="Bold" FontFamily="Cambria" />
<TextBox MinWidth="150" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="InputTextBox"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button x:Name="YesButton" Margin="5" Content="Yes" Background="{x:Null}" Click="YesButton_Click"/>
<Button x:Name="NoButton" Margin="5" Content="No" Background="{x:Null}" Click="NoButton_Click" />
</StackPanel>
</StackPanel>
</Border>
</Grid>
</Grid>
显示此对话框非常简单,因为您只需要将InputBox
网格的可见性设置为可见。然后,您只需处理Yes / No按钮并从TextBox中获取Input文本。
因此,您只需将ShowDialog()
选项设置为Visibility
,而不是使用需要Visible
的代码。我们将在代码隐藏中处理这个示例中仍有一些事情要做,例如在处理Yes / No Button点击后清除InputText框。
代码隐藏:
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void CoolButton_Click(object sender, RoutedEventArgs e)
{
// CoolButton Clicked! Let's show our InputBox.
InputBox.Visibility = System.Windows.Visibility.Visible;
}
private void YesButton_Click(object sender, RoutedEventArgs e)
{
// YesButton Clicked! Let's hide our InputBox and handle the input text.
InputBox.Visibility = System.Windows.Visibility.Collapsed;
// Do something with the Input
String input = InputTextBox.Text;
MyListBox.Items.Add(input); // Add Input to our ListBox.
// Clear InputBox.
InputTextBox.Text = String.Empty;
}
private void NoButton_Click(object sender, RoutedEventArgs e)
{
// NoButton Clicked! Let's hide our InputBox.
InputBox.Visibility = System.Windows.Visibility.Collapsed;
// Clear InputBox.
InputTextBox.Text = String.Empty;
}
}
}
使用Dependency可以轻松完成代码隐藏,或者在这种情况下使用ViewModel逻辑,但为了简单起见,我将其保留在代码隐藏中。
答案 1 :(得分:6)
这是我的解决方案,我花了大约3个小时来输入它。它是完全可定制的。
string var = new InputBox("text").ShowDialog();
这是班级的代码
public class InputBox
{
Window Box = new Window();//window for the inputbox
FontFamily font = new FontFamily("Tahoma");//font for the whole inputbox
int FontSize=30;//fontsize for the input
StackPanel sp1=new StackPanel();// items container
string title = "InputBox";//title as heading
string boxcontent;//title
string defaulttext = "Scrivi quì il tuo nome...";//default textbox content
string errormessage = "Scelta non valida";//error messagebox content
string errortitle="Errore";//error messagebox heading title
string okbuttontext = "OK";//Ok button content
Brush BoxBackgroundColor = Brushes.GreenYellow;// Window Background
Brush InputBackgroundColor = Brushes.Ivory;// Textbox Background
bool clicked = false;
TextBox input = new TextBox();
Button ok = new Button();
bool inputreset = false;
public InputBox(string content)
{
try
{
boxcontent = content;
}
catch { boxcontent = "Error!"; }
windowdef();
}
public InputBox(string content,string Htitle, string DefaultText)
{
try
{
boxcontent = content;
}
catch { boxcontent = "Error!"; }
try
{
title = Htitle;
}
catch
{
title = "Error!";
}
try
{
defaulttext = DefaultText;
}
catch
{
DefaultText = "Error!";
}
windowdef();
}
public InputBox(string content, string Htitle,string Font,int Fontsize)
{
try
{
boxcontent = content;
}
catch { boxcontent = "Error!"; }
try
{
font = new FontFamily(Font);
}
catch { font = new FontFamily("Tahoma"); }
try
{
title = Htitle;
}
catch
{
title = "Error!";
}
if (Fontsize >= 1)
FontSize = Fontsize;
windowdef();
}
private void windowdef()// window building - check only for window size
{
Box.Height = 500;// Box Height
Box.Width = 300;// Box Width
Box.Background = BoxBackgroundColor;
Box.Title = title;
Box.Content = sp1;
Box.Closing += Box_Closing;
TextBlock content=new TextBlock();
content.TextWrapping = TextWrapping.Wrap;
content.Background = null;
content.HorizontalAlignment = HorizontalAlignment.Center;
content.Text = boxcontent;
content.FontFamily = font;
content.FontSize = FontSize;
sp1.Children.Add(content);
input.Background = InputBackgroundColor;
input.FontFamily = font;
input.FontSize = FontSize;
input.HorizontalAlignment = HorizontalAlignment.Center;
input.Text = defaulttext;
input.MinWidth = 200;
input.MouseEnter += input_MouseDown;
sp1.Children.Add(input);
ok.Width=70;
ok.Height=30;
ok.Click += ok_Click;
ok.Content = okbuttontext;
ok.HorizontalAlignment = HorizontalAlignment.Center;
sp1.Children.Add(ok);
}
void Box_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(!clicked)
e.Cancel = true;
}
private void input_MouseDown(object sender, MouseEventArgs e)
{
if ((sender as TextBox).Text == defaulttext && inputreset==false)
{
(sender as TextBox).Text = null;
inputreset = true;
}
}
void ok_Click(object sender, RoutedEventArgs e)
{
clicked = true;
if (input.Text == defaulttext||input.Text == "")
MessageBox.Show(errormessage,errortitle);
else
{
Box.Close();
}
clicked = false;
}
public string ShowDialog()
{
Box.ShowDialog();
return input.Text;
}
}
希望它有用。
答案 2 :(得分:4)
在Visual Studio项目中创建另一个Window类,该类在公共属性中保存用户名。然后在主窗口的某个位置创建此窗口的实例,并使用ShowDialog方法显示它。这会阻塞,直到您的“对话框”窗口关闭。然后,您可以从公共财产中获取用户名,并随意使用它。
答案 3 :(得分:3)
在项目中创建/添加新的Window
以获取用户的输入。然后,您可以使用Window.Show
或Window.ShowDialog
将该窗口显示为弹出窗口
同时添加一个OK
按钮n创建的窗口,然后单击确定按钮单击保存文本文件中的信息
答案 4 :(得分:2)
MSDN上的自定义对话框部分可能会为您提供一些指导:Custom Dialog Box in WPF。还有代码示例和XAML源代码。
处理后,您可以搜索如何将数据保存到文件中 - 这很容易,并且有很多方法可以做到这一点(其中一种方法是使用TextWriter
类:example )。
答案 5 :(得分:0)
谢谢!!我的修改版本:
public class InputBox
{
Window Box = new Window();//window for the inputbox
FontFamily font = new FontFamily("Avenir");//font for the whole inputbox
int FontSize = 14;//fontsize for the input
StackPanel sp1 = new StackPanel();// items container
string title = "Dica s.l.";//title as heading
string boxcontent;//title
string defaulttext = "";//default textbox content
string errormessage = "Datos no válidos";//error messagebox content
string errortitle = "Error";//error messagebox heading title
string okbuttontext = "OK";//Ok button content
string CancelButtonText = "Cancelar";
Brush BoxBackgroundColor = Brushes.WhiteSmoke;// Window Background
Brush InputBackgroundColor = Brushes.Ivory;// Textbox Background
bool clickedOk = false;
TextBox input = new TextBox();
Button ok = new Button();
Button cancel = new Button();
bool inputreset = false;
public InputBox(string content)
{
try
{
boxcontent = content;
}
catch { boxcontent = "Error!"; }
windowdef();
}
public InputBox(string content, string Htitle, string DefaultText)
{
try
{
boxcontent = content;
}
catch { boxcontent = "Error!"; }
try
{
title = Htitle;
}
catch
{
title = "Error!";
}
try
{
defaulttext = DefaultText;
}
catch
{
DefaultText = "Error!";
}
windowdef();
}
public InputBox(string content, string Htitle, string Font, int Fontsize)
{
try
{
boxcontent = content;
}
catch { boxcontent = "Error!"; }
try
{
font = new FontFamily(Font);
}
catch { font = new FontFamily("Tahoma"); }
try
{
title = Htitle;
}
catch
{
title = "Error!";
}
if (Fontsize >= 1)
FontSize = Fontsize;
windowdef();
}
private void windowdef()// window building - check only for window size
{
Box.Height = 100;// Box Height
Box.Width = 450;// Box Width
Box.Background = BoxBackgroundColor;
Box.Title = title;
Box.Content = sp1;
Box.Closing += Box_Closing;
Box.WindowStyle = WindowStyle.None;
Box.WindowStartupLocation = WindowStartupLocation.CenterScreen;
TextBlock content = new TextBlock();
content.TextWrapping = TextWrapping.Wrap;
content.Background = null;
content.HorizontalAlignment = HorizontalAlignment.Center;
content.Text = boxcontent;
content.FontFamily = font;
content.FontSize = FontSize;
sp1.Children.Add(content);
input.Background = InputBackgroundColor;
input.FontFamily = font;
input.FontSize = FontSize;
input.HorizontalAlignment = HorizontalAlignment.Center;
input.Text = defaulttext;
input.MinWidth = 200;
input.MouseEnter += input_MouseDown;
input.KeyDown += input_KeyDown;
sp1.Children.Add(input);
ok.Width = 70;
ok.Height = 30;
ok.Click += ok_Click;
ok.Content = okbuttontext;
cancel.Width = 70;
cancel.Height = 30;
cancel.Click += cancel_Click;
cancel.Content = CancelButtonText;
WrapPanel gboxContent = new WrapPanel();
gboxContent.HorizontalAlignment = HorizontalAlignment.Center;
sp1.Children.Add(gboxContent);
gboxContent.Children.Add(ok);
gboxContent.Children.Add(cancel);
input.Focus();
}
void Box_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//validation
}
private void input_MouseDown(object sender, MouseEventArgs e)
{
if ((sender as TextBox).Text == defaulttext && inputreset == false)
{
(sender as TextBox).Text = null;
inputreset = true;
}
}
private void input_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && clickedOk == false )
{
e.Handled = true;
ok_Click(input, null);
}
if (e.Key == Key.Escape)
{
cancel_Click(input, null);
}
}
void ok_Click(object sender, RoutedEventArgs e)
{
clickedOk = true;
if (input.Text == defaulttext || input.Text == "")
MessageBox.Show(errormessage, errortitle,MessageBoxButton.OK,MessageBoxImage.Error);
else
{
Box.Close();
}
clickedOk = false;
}
void cancel_Click(object sender, RoutedEventArgs e)
{
Box.Close();
}
public string ShowDialog()
{
Box.ShowDialog();
return input.Text;
}
}