教你在UWP中使用MsgBox和InputBox
vb.net吧
全部回复
仅看楼主
level 13
Nukepayload2 楼主
一楼不喂熊,直接上教程和链接!
这篇文章我首次发布是在
http://www.cnblogs.com/Nukepayload2/p/uwp_libfuncs_remake_vbnet.html
在博客可以看代码的解释。
使用MsgBox前,把这段代码放到某个模块里面
Public Async Function MsgBoxAsync(Prompt$, HasCancel As Boolean, Title$, Optional OK$ = "确定", Optional Cancel$ = "取消") As Task(Of Boolean?)
Dim dlg As New MessageDialog(Prompt, Title)
Dim Result As Boolean?
If HasCancel Then
Dim msg As New MessageDialog(Prompt, Title)
msg.Commands.Add(New UICommand(OK, Sub(command) Result = True))
msg.Commands.Add(New UICommand(Cancel, Sub(command) Result = False))
msg.DefaultCommandIndex = 0
msg.CancelCommandIndex = 1
Dim tsk = msg.ShowAsync
Await tsk
Return Result
Else
Await New MessageDialog(Prompt, Title).ShowAsync
Return True
End If
End Function
这是个精简的MsgBox,可以单按钮和双按钮,能修改按钮上的文字。
用法示例:
Await MsgBoxAsync("未找到VeteranSpeed的符号", False, "未找到")
InputBox稍微麻烦一点。先新建xaml对话框
<ContentDialog
x:Class="Nukepayload2.VisualBasicExtensions.UWP.InputBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<ContentDialog.Content>
<Grid MinWidth="100" MinHeight="100">
<Grid.RowDefinitions>
<RowDefinition Height="13*"/>
<RowDefinition Height="7*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="71*"/>
<ColumnDefinition Width="19*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="TxtPrompt"></TextBlock>
<TextBox x:Name="TxtOutput" Grid.Row="1"></TextBox>
<Button Grid.Row="1" Grid.Column="1" Click="BtnOk_Click">确定</Button>
</Grid>
</ContentDialog.Content>
</ContentDialog>
然后写逻辑代码
Public NotInheritable Class InputBox
Inherits ContentDialog
Public Overloads Async Function ShowAsync(Prompt As String, Title As String, Optional InputScope As InputScopeNameValue = InputScopeNameValue.Text) As Task(Of String)
Me.Title = Title
TxtPrompt.Text = Prompt
TxtOutput.Text = ""
TxtOutput.InputScope = New InputScope
TxtOutput.InputScope.Names.Add(New InputScopeName(InputScope))
Await ShowAsync()
Return TxtOutput.Text
End Function
Private Sub BtnOk_Click(sender As Object, e As RoutedEventArgs)
Hide()
End Sub
End Class
在模块里把它变成静态的
Dim inputbox As New InputBox()
Public Async Function InputBoxAsync(Prompt$, Title$, Optional InputScope As InputScopeNameValue = InputScopeNameValue.Text) As Task(Of String)
Return Await inputbox.ShowAsync(Prompt, Title, InputScope)
End Function
一个能够显示帮助文本,能设置虚拟键盘的精简版InputBox诞生了!
用法示例:
Dim YourName=Await InputBox("你叫什么名字?", "输入姓名")
我的博客里还写了StrConv和Rnd怎么实现。想用它们的话就进博客看代码吧。
---贴吧极速版 For UWP
2016年03月07日 04点03分 1
level 9
这样就可以另开一个弹窗了是么
2016年03月07日 07点03分 2
这个方式取决于设备和你的实现。在电脑确实新建了一个弹窗。 ---贴吧极速版 For UWP
2016年03月07日 14点03分
level 13
msgbox一般都用Await new messagedialog("").showasync 代替了嘛,多按钮还有input的话直接contentdialog。
用flyout糊一个inputbox也不麻烦啊[冷]
---贴吧极速版 For UWP
2016年03月08日 14点03分 3
你有没有想过移植一个WPF程序?移植的时候需要把缺少的运行库函数补上。 ---贴吧极速版 For UWP
2016年03月08日 15点03分
level 9
我发现你现在喜欢用 $ 来声明字符串,在博客园你发的 ASP.NET 也是的。
---贴吧极速版 For UWP
2016年03月08日 16点03分 5
懒得打字而已 ---贴吧极速版 For UWP
2016年03月09日 00点03分
要是我不再介意多敲几下键盘,我可能会更多地使用c#。 ---贴吧极速版 For UWP
2016年03月09日 00点03分
level 8
正好最近要用对话框。。直接就拿来玩了[滑稽]
2016年03月12日 16点03分 6
1