如何在 .NET 7 MAUI 中使用 XAML 使标签可选?

huangapple go评论62阅读模式
英文:

How to make a Label Selectable in .NET 7 MAUI with XAML?

问题

我正在创建一个密码生成器,但我不知道如何使标签(程序放置生成的密码的地方)可选择但不可编辑... 如何做到这一点?

这是我的 mainpage.xaml 代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="PassGen.MainPage">
    
    <ScrollView
        BackgroundColor="DarkSlateGray">
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

            <Label
                x:Name="PasswordLabel"
                HorizontalOptions="Center"
                Text="单击“生成”以生成密码"
                FontAttributes="Italic"></Label>

            <Button
                BackgroundColor="Coral"
                Text="生成"
                Clicked="GeneratePassword"></Button>
            
        </VerticalStackLayout>
    </ScrollView>

</ContentPage>
英文:

I am creating a password generator and I don't know how to make the label (where the program puts the generated password) selectable but not editable... How to do it ?

Here is my mainpage.xaml code :

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="PassGen.MainPage">
    

    <ScrollView
        BackgroundColor="DarkSlateGray">
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

            <Label
                x:Name="PasswordLabel"
                HorizontalOptions="Center"
                Text="Click Generate to generate a password"
                FontAttributes="Italic"
                
           
            ></Label>


            <Button
                BackgroundColor="Coral"
                Text="Generate"
                Clicked="GeneratePassword"
            ></Button>
            

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

答案1

得分: 2

可以尝试在标签上添加点击手势,并获取标签的文本,或者添加按钮以获取标签的文本。

我有两种方法来实现这个功能。您可以参考以下代码:

public class MyViewModel: INotifyPropertyChanged 
{
    public ICommand TapCommand { get; set; }

    public ICommand GetPwdCommand { get; set; }


    private string _password ="123456";
    public string Password
    {
        get => _password;
        set
        {
            SetProperty(ref _password, value);
        }
    }

    public MyViewModel()
    {

        TapCommand = new Command<object>(OnLabelTapped);

        GetPwdCommand = new Command(getPwdFromEntry);

    }

    private void getPwdFromEntry()
    {
        System.Diagnostics.Debug.WriteLine("method 1-----> Password = " + Password);

    }

    private void OnLabelTapped(object obj)
    {
        if (obj is Label )
        {
            var label = (Label)obj;

            string str = label.Text;

            System.Diagnostics.Debug.WriteLine("method 2--- >Password = " + str);
        }
    }
}

用法示例:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiMvvmApp226.CollectionViewPage"
             xmlns:MauiMvvmApp="clr-namespace:MauiMvvmApp"
             Title="CollectionViewPage">

    <ContentPage.BindingContext>
        <MauiMvvmApp:MyViewModel></MauiMvvmApp:MyViewModel>
    </ContentPage.BindingContext>

    <VerticalStackLayout>
        <Label
                x:Name="PasswordLabel"
                HorizontalOptions="Center"
                Text="Click Generate to generate a password" HeightRequest="50"
                >
            <Label.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding TapCommand}" CommandParameter="{Binding Source={x:Reference PasswordLabel}}">
                </TapGestureRecognizer>
            </Label.GestureRecognizers>
        </Label>

        <Entry  x:Name="mEntry" IsReadOnly="True"  Text="{Binding Password}" />
        <Button
                Command="{Binding GetPwdCommand}"
                BackgroundColor="Coral"
                Text="Generate">
        </Button>
    </VerticalStackLayout>
</ContentPage>
英文:

Yes, you can try to add a tap gesture to your Label and get the text of the Label or add a button to get the text of the Label.

I have used two ways to achieve this function. You can refer to the following code:

public class MyViewModel: INotifyPropertyChanged 
{
    public ICommand TapCommand { get; set; }

    public ICommand GetPwdCommand { get; set; }


    private string _password =&quot;123456&quot;;
    public string Password
    {
        get =&gt; _password;
        set
        {
            SetProperty(ref _password, value);
        }
    }

    public MyViewModel()
    {

        TapCommand = new Command&lt;object&gt;(OnLabelTapped);

        GetPwdCommand = new Command(getPwdFromEntry);

    }

    private void getPwdFromEntry()
    {
        System.Diagnostics.Debug.WriteLine(&quot;method 1-----&gt; Password = &quot; + Password);

    }

    private void OnLabelTapped(object obj)
    {
        if (obj is Label )
        {
            var label = (Label)obj;

            string str = label.Text;

            System.Diagnostics.Debug.WriteLine(&quot;method 2--- &gt;Password = &quot; + str);
        }
    }
}

Usage example:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt; 
&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
             x:Class=&quot;MauiMvvmApp226.CollectionViewPage&quot;
             xmlns:MauiMvvmApp=&quot;clr-namespace:MauiMvvmApp&quot;
             Title=&quot;CollectionViewPage&quot;&gt;

    &lt;ContentPage.BindingContext&gt;
        &lt;MauiMvvmApp:MyViewModel&gt;&lt;/MauiMvvmApp:MyViewModel&gt;
    &lt;/ContentPage.BindingContext&gt;

    &lt;VerticalStackLayout&gt;
        &lt;Label
                x:Name=&quot;PasswordLabel&quot;
                HorizontalOptions=&quot;Center&quot;
                Text=&quot;Click Generate to generate a password&quot; HeightRequest=&quot;50&quot;
                &gt;
            &lt;Label.GestureRecognizers&gt;
                &lt;TapGestureRecognizer Command=&quot;{Binding TapCommand}&quot; CommandParameter=&quot;{Binding Source={x:Reference PasswordLabel}}&quot;&gt;
                &lt;/TapGestureRecognizer&gt;
            &lt;/Label.GestureRecognizers&gt;
        &lt;/Label&gt;


        &lt;Entry  x:Name=&quot;mEntry&quot; IsReadOnly=&quot;True&quot;  Text=&quot;{Binding Password}&quot; /&gt;
        &lt;Button
                Command=&quot;{Binding GetPwdCommand}&quot;
                BackgroundColor=&quot;Coral&quot;
                Text=&quot;Generate&quot;&gt;
        &lt;/Button&gt;


    &lt;/VerticalStackLayout&gt;
&lt;/ContentPage&gt;

huangapple
  • 本文由 发表于 2023年4月20日 01:26:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定