Common vb library functions and uwpvb library functions are revived in uwp.

Source: Internet
Author: User

Common vb library functions and uwpvb library functions are revived in uwp.

This blog post is purely original. It must be reproduced by Nukepayload2 !!

In. Net Core, it has been simplified in many places, and a major hit area is the vb language library. From the initial functions that included vb6 library functions and later bound to the current several functions plus later binding, the library functions corresponding to the End and Mid statements were deleted.

Some functions should not be deleted. If you want to use it, you have to manually restore it.

First, the MsgBox and InputBox functions that are popular in the Hello world.

They are in the Interaction of Microsoft. VisualBasic.

Create a new module called Interaction.

Analyze MsgBox first. This function is used to display the title, content, and option buttons in a pop-up window.

Two buttons and one button are commonly used. The help is not commonly used, and the three buttons are not commonly used.

So we can implement one or two buttons. If the return value is True, the confirmation is followed. If the return value is False, the dialog box is forcibly closed.

Because it is uwp, it is difficult to implement the synchronization version. Write an asynchronous version.

Public Async Function MsgBoxAsync (Prompt $, HasCancel As Boolean, Title $, Optional OK $ = "OK", Optional Cancel $ = "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

Next it's the turn of InputBox.

This is used to collect input, a text box, a prompt, two commonly used buttons, and a button that is not commonly used for help.

I still don't need to write the Help button to save time. The cancel button is of little use and does not need to be written, because the UWP text box comes with the clear button.

First, create a dialog box and write this in The Xaml code:

<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"> OK </Button> </Grid> </ContentDialog. content> </ContentDialog>

The following vb Code (considering the virtual keyboard ):

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 SubEnd Class

My control naming method is a bit odd. You can write it according to your habits.

Finally, write the code for this function in the Interaction module.

    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

There are also some common ones, such as Rnd and Randomize

These two Random number functions are more convenient than directly using the Random class, especially when calculating single-precision floating point numbers.

Here I didn't use BitConverter to convert Single and Integer, but to use a consortium. When you implement BitConverter, you can try BitConverter.

Also, the default value 0 can be used as the seed.

Imports System.Runtime.InteropServicesPublic Module VBMath    Dim rand As New RandomPublic Sub Randomize()        rand = New Random    End Sub    Public Sub Randomize(Number As Single)        rand = New Random(New SingleInt32(Number).Int32Value)    End Sub    Public Function Rnd() As Single        Return rand.NextDouble    End Function    Public Function Rnd(Number As Single) As Single        rand = New Random(New SingleInt32(Number).Int32Value)        Return Rnd    End Function    Private Structure SingleInt32        Sub New(SingleValue!)            Me.SingleValue = SingleValue        End Sub        Sub New(Int32Value%)            Me.Int32Value = Int32Value        End Sub        <FieldOffset(0)>        Dim SingleValue!        <FieldOffset(0)>        Dim Int32Value%    End StructureEnd Module

The functions to be implemented are used in game role name processing and sensitive word processing.

Multi-function string converter StrConv

It can be used for both simplified and traditional conversion, full-width half-width conversion, and special case-sensitive format conversion.

I checked msdn before writing, and only one local function can be found above to replace it. Then I will use it to implement StrConv.

This is the declaration. You should check it before writing it. This function is declared in Windows. h.

int LCMapStringEx(  _In_opt_  LPCWSTR          lpLocaleName,  _In_      DWORD            dwMapFlags,  _In_      LPCWSTR          lpSrcStr,  _In_      int              cchSrc,  _Out_opt_ LPWSTR           lpDestStr,  _In_      int              cchDest,  _In_opt_  LPNLSVERSIONINFO lpVersionInformation,  _In_opt_  LPVOID           lpReserved,  _In_opt_  LPARAM           sortHandle);

After reading it, I started to make a fuss!

Create a new component for windows runtime, and then create it to the class. Write this:

static String^ LCMapString(String^ LocaleName, int MapFlags, String^ Source){     int len = Source->Length();     auto str = ref new String(new wchar_t[len + 1], len);     LCMapStringEx(LocaleName->Begin(), MapFlags, Source->Begin(), len, const_cast<wchar_t*>(str->Begin()), len, NULL, NULL, NULL);     return str;}

The c ++/cx code above and below only conveys the meaning and has not been tested.

static String^ LCMapString(int MapFlags, String^ Source){     len = Source->Length();     auto str = ref new String(new wchar_t[len + 1], len);     LCMapStringEx(LOCALE_NAME_USER_DEFAULT, MapFlags, Source->Begin(), len, const_cast<wchar_t*>(str->Begin()), len, NULL, NULL, NULL);     return str;}

Note that the MapFlags In the parameter is the same as the local LCMapStringEx, and the constant of vb needs to be converted to the mark value.

First, define the VbStrConv enumeration. This part can also be completed using vb.

            public enum class VbStrConv            {                Hiragana = 0x20,                Katakana = 0x10,                LinguisticCasing = 0x400,                Lowercase = 2,                Narrow = 8,                None = 0,                ProperCase = 3,                SimplifiedChinese = 0x100,                TraditionalChinese = 0x200,                Uppercase = 1,                Wide = 4            };

Then write the conversion function. If the enumeration is implemented in vb, the conversion function is also written in vb.

Static int VbStrConvToMapFlags (VbStrConv value) {int val = static_cast <int> (value); int flag = 0; // if (val & VbStrConv: Wide = VbStrConv :: wide) {flag & = LCMAP_FULLWIDTH;} else if (val & VbStrConv: Narrow = VbStrConv: Narrow) {flag & = LCMAP_HALFWIDTH ;} // if (val & VbStrConv: ProperCase = VbStrConv: ProperCase) {flag & = LCMAP_TITLECASE;} else if (val & VbStrConv: Uppercase = VbStrConv :: uppercase) {flag & = flag;} else if (val & VbStrConv: Lowercase = VbStrConv: Lowercase) {flag & = LCMAP_LOWERCASE;} else if (val & VbStrConv :: linguisticCasing = VbStrConv: LinguisticCasing) {flag & = container;} // if (val & VbStrConv: Hiragana = VbStrConv: Hiragana) {flag & = container ;} else if (val & VbStrConv: Katakana = VbStrConv: Katakana) {flag & = LCMAP_KATAKANA;} // if (val & VbStrConv: SimplifiedChinese = VbStrConv :: simplifiedChinese) {flag & = flag;} else if (val & VbStrConv: TraditionalChinese = VbStrConv: TraditionalChinese) {flag & = LCMAP_TRADITIONAL_CHINESE;} return flag ;}

The rest of the work is handed over to vb. The Locale name is not the same as the previous library function. Previously, LCID was used. After re-implementation, strings are used, making it easier to work with existing localized APIs.

Imports Nukepayload2.VisualBasicExtensions.UWP.Native.StringsPublic Module Strings    Public Function StrConv$(Source$, Conversion As VbStrConv)        Return LCMapString(VbStrConvToMapFlags(Conversion), Source)    End Function    Public Function StrConv$(Source$, Conversion As VbStrConv, LocaleName$)        Return LCMapString(LocaleName, VbStrConvToMapFlags(Conversion), Source)    End FunctionEnd Module

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.