code-behind 相关问题

代码隐藏是指包含在单独的类文件中的UI代码(Windows窗体,ASP.NET页面等)。这允许分离UI及其背后的逻辑。


如何将换行符放入文本框中,该换行符仅在收到字符串“CRLF”时发生

如何将换行符放入文本框中,该换行符仅在收到字符串“CRLF”时发生 这是我的文本框输出 显示 CRLF?J] 的行应该只是一个空行。 这是

回答 2 投票 0

以编程方式构建的按钮不会出现在主窗口中

背景 我有一个风险游戏学习项目的 MVVM 框架。我决定尝试以编程方式创建 UI 的一部分,既“方便”/优雅,又作为学习

回答 0 投票 0

Button_click 上的 VBnet 需要运行代码隐藏功能然后打开模态对话框

我有一个应用程序,其中选择了 gridview 数据,完成后,用户单击一个按钮继续。那时我需要在代码隐藏中运行一个函数来处理所选项目。一次

回答 1 投票 0

如何智能地创建和调试嵌套堆栈布局的复杂 Maui 数据绑定?

我一直在尝试创建一个数据绑定来访问父堆栈布局中的值。 我创建了一个转换器,认为这可以让我了解我的绑定在做什么。

回答 0 投票 0

如何使用 Visual Studio“合并”XAML 文件及其代码隐藏

我在名为“MyTemplate.xaml”的 XAML 文件中定义了一个模板。此模板使用名为“MyTemplate.cs”的代码隐藏文件。 我的模板.xaml: 我在名为“MyTemplate.xaml”的 XAML 文件中定义了一个模板。此模板使用名为“MyTemplate.cs”的代码隐藏文件。 Mytemplate.xaml: <ResourceDictionary x:Class="Project.Templates.MyTemplate"> <DataTemplate ... /> </ResourceDictionary> MyTemplate.cs: namespace Project.Templates { public partial class MyTemplate : ResourceDictionary { ... } } 在 Visual Studio 解决方案资源管理器中,这两个文件并排放置。我想做的是将这两个文件放在一起,就像使用控件及其代码隐藏一样。 我有什么: 我想要的: 最好的方法是什么?谢谢。 您需要编辑.csproj 文件。找到MyTemplate.cs的<Compile>元素,在其下添加一个<DependentUpon>元素: <Compile Include="MyTemplate.cs"> <DependentUpon>MyTemplate.xaml</DependentUpon> </Compile> 请参阅此博客文章:使一个项目成为另一个项目的子项目 这不是对您最初问题的回答,而是对这个问题的回答: 在那种情况下,请解释如何在不使用代码隐藏的情况下将事件处理程序添加到模板 您可以使用 ViewModel 和 ICommand 类执行此操作。 首先,您需要创建您的 ViewModel 类,使用无参数构造函数使其公开且非静态。 然后创建另一个实现 ICommand 接口的类: public class Command : ICommand { public void Execute(object parameter) { //this is what happens when you respond to the event } public bool CanExecute(object parameter) { return true; } public event EventHandler CanExecuteChanged; } 将命令类的实例添加到 ViewModel 类,将其设为私有并通过只读属性将其公开: public class ViewModel { private readonly ICommand _command = new Command(); public ICommand Command { get { return _command; } } } 在 App.xaml 文件中将您的 ViewModel 添加为静态资源: <Application.Resources> <wpfApplication1:ViewModel x:Key="ViewModel"/> </Application.Resources> 将您的 XAML 文件的 DataContext 设置为您的 ViewModel: <Window DataContext="{StaticResource ViewModel}"> 现在通过绑定到 Command 类来响应您的事件: <Button Click="{Binding Command}"></Button> 繁荣,没有代码隐藏。希望这会有所帮助。 另一种方式是: 添加/创建新的 XAML 文件/项目 将旧的 .xaml 和 xaml.cs 内容复制并粘贴到新的等效文件 删除单独的文件 重命名新文件 最简单的方法是: 从项目中排除未链接的文件 确保在解决方案资源管理器中启用“显示所有文件” 选择容器文件(本例中为.xaml 而不是xaml.cs),右键单击并包含在项目中。应该添加到项目中并修复链接。

回答 4 投票 0

在嵌套的 StackLayout 中使用 Maui XAML 绑定 InvHeader/InvItems,其中 InvHeader.CustId = InvItem.CustId?

我正在尝试在 Maui 中创建一个母版/详细信息页面。就像带有发票项目的发票一样,此页面必须显示多个“发票”,每个发票都有自己的发票项目。 假设我们有一个清单......

回答 2 投票 0

如何在多个页面和类之间共享代码

如果我根据论坛的规则做错了,请原谅我,但我不知道该怎么做。这是对以下链接中提出的问题的修订。那里的反应接近...

回答 2 投票 0

如何在页面和类之间共享代码

这看起来应该很简单,但我想不通。我正在尝试在代码隐藏页面和继承页面类的类之间共享代码。 重要的一点是我需要...

回答 1 投票 0

单击按钮后激活 CharactersValidationBehavior

单击按钮后,如何激活或重新评估为条目设置的 CharactersValidationBehavior 的结果? 单击按钮后,如何激活或重新评估为条目设置的 CharactersValidationBehavior 的结果? <Entry.Behaviors> <toolkit:CharactersValidationBehavior IsValid="{Binding InputValid}" Flags="ValidateOnValueChanged" CharacterType="Alphanumeric" MinimumCharacterTypeCount="1" /> </Entry.Behaviors> 非可选条目在 XAML 中正确设置,并在 ContentView 显示后立即在 ContentView 的代码隐藏中的 InputValid-Property 中进行评估。但我真正想要的是,在单击按钮后立即进行评估。 我怎么能这样做? 可以添加按钮事件进行点击后验证,然后定义Entry控件的焦点事件。当Entry获得焦点时,去掉验证,这样就可以达到点击激活或者重新评价的目的。我写了一个demo来测试一下。可以参考下面的代码 Xaml文件: <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit" x:Class="MauiApp7.MainPage"> <ContentPage.Resources> <Style x:Key="InvalidEntryStyle" TargetType="Entry" x:Name="invalidStyle"> <Setter Property="TextColor" Value="Red" /> </Style> <Style x:Key="ValidEntryStyle" TargetType="Entry" x:Name="validStyle"> <Setter Property="TextColor" Value="Green" /> </Style> </ContentPage.Resources> <StackLayout> <Entry x:Name="enrty" Focused="OnEntryFocused" > <Entry.Behaviors> <toolkit:CharactersValidationBehavior x:Name="charactersValidationBehavior"/> </Entry.Behaviors> </Entry> <Button Text="Evaluate" Clicked="OnEvaluated"/> </ContentPage> .CS文件: using CommunityToolkit.Maui.Behaviors; namespace MauiApp7; public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); } private void OnEvaluated(object sender, EventArgs e) { charactersValidationBehavior.InvalidStyle = invalidStyle; charactersValidationBehavior.ValidStyle = validStyle; charactersValidationBehavior.Flags = ValidationFlags.ValidateOnValueChanged; charactersValidationBehavior.CharacterType = CharacterType.Digit; charactersValidationBehavior.MinimumCharacterTypeCount = 3; enrty.Behaviors.Add(charactersValidationBehavior); } private void OnEntryFocused(object sender, EventArgs e) { enrty.Behaviors.Remove(charactersValidationBehavior); } } 我试图实现你的基本概念,但它似乎没有像我想要的那样工作。 基本上,我想稍微修改一下,当用户在相应条目中输入的内容未正确验证时,我想显示一个图标或一个带有文本的标签。 在我的例子中,我有一个 ContentPage 使用几个 ContentViews 来承载条目和标签,还有应该以红色或类似的东西显示错误消息的标签。 内容页: <?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" xmlns:viewModels="clr-namespace:MyApp.ViewModels" xmlns:controls="clr-namespace:MyApp.Pages.Views" x:DataType="viewModels:ManageItemsViewModel" x:Class="MyApp.Pages.ManageItems" Title="Some title"> <VerticalStackLayout> <Label Text="Addn Items" FontAttributes="Bold" VerticalOptions="Center" HorizontalOptions="Center" Padding="20" /> <StackLayout> <controls:MyItemInputControlView NameLabelString="Label for Input 1:" IsOptionalLabelString="Mandatory" PlaceholderString="a placeholder 1" EntryInput="{Binding Item.Name}" InputValid="{Binding NameInput1Valid, Mode=TwoWay}" /> <controls:MyItemInputControlView NameLabelString="Label for Input 2:" IsOptionalLabelString="Optional" PlaceholderString="a placeholder 2" EntryInput="{Binding Item.Brand}" InputValid="{Binding NameInput2Valid, Mode=TwoWay}" /> <StackLayout Margin="20, 50, 15, 0"> <Grid RowSpacing="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Button Grid.Row="0" Grid.Column="0" Margin="5" Text="OK" Command="{Binding SaveItemCommand}" /> <Button Grid.Row="0" Grid.Column="1" Margin="5" Text="Cancel" Command="{Binding CancelItemCommand}" /> </Grid> </StackLayout> </StackLayout> </VerticalStackLayout> </ContentPage> 内容视图: <?xml version="1.0" encoding="utf-8" ?> <ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit" xmlns:views="clr-namespace:MyApp.Pages.Views" x:Class="MyApp.Pages.Views.MyItemInputControlView" x:Name="this"> <ContentView.Resources> <ResourceDictionary> <toolkit:InvertedBoolConverter x:Key="InvertBoolValueConverter" /> </ResourceDictionary> </ContentView.Resources> <StackLayout BindingContext="{x:Reference this}"> <Grid Margin="20, 0, 20, 0"> <Grid.Resources> <!--<Style TargetType="Entry"> <Setter Property="Padding" Value="2 1" /> <Setter Property="BorderBrush" Value="LightGray" /> </Style>--> </Grid.Resources> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <StackLayout Grid.Row="0" Grid.Column="0" VerticalOptions="Center"> <Label Text="{Binding NameLabelString}" /> <Label Text="{Binding IsOptionalLabelString}" FontSize="12" /> </StackLayout> <StackLayout Grid.Row="0" Grid.Column="1" VerticalOptions="Center" > <Entry Text="{Binding EntryInput}" Placeholder="{Binding PlaceholderString}" Keyboard="{Binding KeyboardSetting}" Margin="5, 0, 5, 15" x:Name="entryControl"> <Entry.Behaviors> <!--<toolkit:CharactersValidationBehavior IsValid="{Binding InputValid}" Flags="ValidateOnUnfocusing,ValidateOnValueChanged" CharacterType="Alphanumeric" MinimumCharacterTypeCount="1" />--> </Entry.Behaviors> </Entry> <Label Text="{Binding NameLabelString}" TextColor="Red" IsVisible="{Binding InputValid, Converter={StaticResource InvertBoolValueConverter}}"/> </StackLayout> <StackLayout Grid.Row="0" Grid.Column="2" VerticalOptions="Center" > <Label Text="{Binding MeasurementUnitString}"/> </StackLayout> </Grid> </StackLayout> </ContentView> ContentView 代码隐藏: namespace MyApp.Pages.Views; public partial class MyItemInputControlView : ContentView { public static readonly BindableProperty NameLabelProperty = BindableProperty.Create(nameof(NameLabelString), typeof(string), typeof(FoodItemInputControlView), string.Empty, BindingMode.OneWay); public static readonly BindableProperty IsOptionalProperty = BindableProperty.Create(nameof(IsOptionalLabelString), typeof(string), typeof(FoodItemInputControlView), string.Empty); public static readonly BindableProperty PlaceholderProperty = BindableProperty.Create(nameof(PlaceholderString), typeof(string), typeof(FoodItemInputControlView), string.Empty); public static readonly BindableProperty MeasurementUnitProperty = BindableProperty.Create(nameof(MeasurementUnitString), typeof(string), typeof(FoodItemInputControlView), string.Empty); public static readonly BindableProperty KeyboardSettingProperty = BindableProperty.Create(nameof(KeyboardSetting), typeof(Keyboard), typeof(FoodItemInputControlView), Keyboard.Default); public static readonly BindableProperty EntryInputProperty = BindableProperty.Create(nameof(EntryInput), typeof(string), typeof(FoodItemInputControlView), string.Empty, BindingMode.TwoWay); public static readonly BindableProperty InputValidProperty = BindableProperty.Create(nameof(InputValid), typeof(bool), typeof(FoodItemInputControlView), true, BindingMode.OneWayToSource); public string NameLabelString { get => (string)GetValue(NameLabelProperty); set => SetValue(NameLabelProperty, value); } public string IsOptionalLabelString { get => (string)GetValue(IsOptionalProperty); set => SetValue(IsOptionalProperty, value); } public string PlaceholderString { get => (string)GetValue(PlaceholderProperty); set => SetValue(PlaceholderProperty, value); } public string MeasurementUnitString { get => (string)GetValue(MeasurementUnitProperty); set => SetValue(MeasurementUnitProperty, value); } public Keyboard KeyboardSetting { get => (Keyboard)GetValue(KeyboardSettingProperty); set => SetValue(KeyboardSettingProperty, value); } public string EntryInput { get => (string)GetValue(EntryInputProperty); set => SetValue(EntryInputProperty, value); } public bool InputValid { get => (bool)GetValue(InputValidProperty); set { // Before everything is initialized TextValidation should be valid if ((string.IsNullOrEmpty(IsOptionalLabelString) || IsOptionalLabelString.Equals("Optional"))) { value = true; } SetValue(InputValidProperty, value); } } public MyItemInputControlView() { InitializeComponent(); this.Loaded += MyItemInputControlView_Loaded; WeakReferenceMessenger.Default.Register<SaveButtonClickedMessage>(this, HandleSaveButtonClickedMessage); } private void HandleSaveButtonClickedMessage(object recipient, SaveButtonClickedMessage message) { var binding = new Binding("IsValid", BindingMode.TwoWay); charactersValidationBehavior.SetBinding(InputValidProperty, binding); charactersValidationBehavior.Flags = ValidationFlags.ValidateOnValueChanged | ValidationFlags.ValidateOnAttaching; charactersValidationBehavior.CharacterType = CharacterType.Alphanumeric; charactersValidationBehavior.MinimumCharacterTypeCount = 1; entryControl.Behaviors.Add(charactersValidationBehavior); } private void MyItemInputControlView_Loaded(object sender, EventArgs e) { entryControl.Behaviors.Remove(charactersValidationBehavior); } } ContentPage 的 ViewModel: using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using MacroCalculatorApp.Models; using SQLite; using System.ComponentModel; namespace MyApp.ViewModels { public partial class ManageItemViewModel : ObservableObject, INotifyPropertyChanged { [ObservableProperty] private MyItem myItem; private bool InputsValid => true; // needs further evaluation if everything on the ContentView is correct [RelayCommand] async Task SaveItem() { WeakReferenceMessenger.Default.Send(new SaveButtonClickedMessage(true)); try { if (InputsValid) { ... // Just navigate to the previous page await Shell.Current.GoToAsync(".."); } } catch (SQLiteException sqliteException) { ... } catch (Exception e) { await App.Current.MainPage.DisplayAlert("Error", e.Message, "OK"); } } [RelayCommand] async Task CancelItem() { // Just navigate to the previous page await Shell.Current.GoToAsync(".."); } public ManageMyItemViewModel() { MyItem = new MyItem(); } } } 我向 ContentView 的代码隐藏发送一个简单的消息(仅包含值 true)以检测按钮单击。 不幸的是,当我分配 CharactersValidationBehavior 时,它不会评估 ContentViews 的条目。此外,绑定“InputValid”-Property 的值在调试时可以为 false,然后当显示 ContentView 时标签可见并显示红色错误消息。我可以用标签本身的“Mode=TwoWay”选项来抑制这一点。 任何想法,我在这里做错了什么?

回答 2 投票 0

如何在模板中对输入绑定进行程序化绑定?

我需要在ListBoxItems的InputBindings中添加一个MouseBinding。我知道如何在xaml中实现。

回答 2 投票 0

如何访问Xamarin.Forms中创建的组件C#代码后面?

所以,我在Xamarin.Forms中做了一个应用程序,当一个按钮被点击时,会创建一些组件和布局。我的问题是,我需要在之后访问这些组件,这样我就可以在 ...

回答 1 投票 1

将Blazor代码移至代码后 - 简单的例子

我刚开始看Blazor教程,正试图掌握代码。我有一个剃须刀页面,看起来像这样(来自教程)。@page "counter"

回答 1 投票 0

在Xmarin.Forms中的ViewCell中切换CheckBox。

我想知道如何在ListView-ViewCell中的item tapped事件中切换当前的复选框。

回答 2 投票 0

在Xamarin.Forms中使用C#代码访问控件

我无法从C#代码背后的代码访问xaml文件中定义的标签。我尝试了x:name =“ l”属性,但它告诉我“未定义'l'”:谁能帮助我如何访问控件...

回答 1 投票 2

使用C#作为背景代码在Xamarin表单的框架内创建StackLayout

我在我的xaml页面中使用 这样: [[[[[[ ...

回答 1 投票 2

如何在Xamarin.Forms C#代码背后创建堆叠的'VisualElements'数组?

我需要跟踪在Xamarin.Forms xaml中创建的20个“标题”,如下所示: [[[[]]] 为您的图块创建自定义ContentView 然后输入代码 List<CustomTile> tileGrid = new List<CustomTile>(); for (int ndx = 0; ndx < 20; ndx++) { var tile = new CustomTile() { ... }; // add them to the visual grid myGrid.Children.Add(tile,col,row); // also add to your grid list tileGrid.Add(tile); }

回答 1 投票 0

我应该能够从C#例程的真实后页中调用?

我有一个托管有ASP.NET Core的Blazor WebAssembly应用,我的标记代码在Index.razor中,后面的代码在Index.razor.cs中。叫我老式。我在...

回答 1 投票 0

将参数传递给AJAX模态弹出窗口?

[因此,经过一些摸索,我在AJAX弹出窗口中运行了一个aspx页面(使用iframe)。但是,此aspx页接受几个参数。该程序用于打开JavaScript弹出窗口以发送...

回答 1 投票 0

在后面的代码中更改Canvas.Left属性吗?

我在XAML中有一个矩形,并且想要在后面的代码中更改其Canvas.Left属性:

回答 3 投票 96

© www.soinside.com 2019 - 2024. All rights reserved.