英文:
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 ="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);
}
}
}
Usage example:
<?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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论