readonly 相关问题

只读是一个通用的概念,意思是“不可写” - 请不要使用此标签。此标记含糊不清,需要分解。

从仅读取文件夹中的nextjs,在其他地方存储缓存数据?

它的工作正常,但NextJS试图写入(Web root)/。下一个文件夹(/home/site/wwwroot)来刷新静态道具和页面。

回答 1 投票 0


用私有阅读变量和对象的c#中的对象的深副本

这是我上一个问题的后续问题,在c#中的同意义中进行了深度副本。我想克隆我的患者对象,以使对象患者covariates是sam ...

回答 1 投票 0


如何在c#?

如何在C#中声明局部常数? 像在Java中一样,您可以执行以下操作: public void f(){ 最终int n = getnum(); // n声明恒定 } 如何在C#中做同样的事情?我尝试了readonl ...

回答 5 投票 0

为什么 C# 8.0 允许结构体中存在只读成员,但不允许类中存在只读成员?

C# 8.0 在结构中引入了只读成员(如此处所述)。例如,您可以使用以下方法: public readonly override string ToString() => $"({X}, {Y}) 是距

回答 1 投票 0

如何将选择元素设置为只读(“禁用”不会在服务器上传递选择值)

当我使用以下任何代码时,选择元素看起来确实被禁用,但选择没有在服务器上传递:我正在考虑使用只读,但我不知道或者这会解决...

回答 6 投票 0

非图像 OpenCL 参数上的 read_only 与 const

阅读 OpenCL 文档,我知道访问限定符 read_only 和 write_only 用于图像内存。 然而,我注意到有些人在常规的、非 i 的场合使用这些限定词......

回答 1 投票 0

如何在可编辑和不可编辑模式之间切换标记输入

我知道有例子,但我似乎不够聪明,无法弄清楚该怎么做。 我正在开发一个 JavaScript 应用程序,用户可以在其中创建带有标签的笔记。我正在使用 Tagify 库...

回答 1 投票 0

不可能实现不可变的结构类型

类字符串是不可变的。不可能实现不可变的结构类型,因为您无法禁用或覆盖赋值操作 (=)。即使您定义了只读结构,您也可以随时更改...

回答 2 投票 0

不可能实现不可变的结构类型

类字符串是不可变的。不可能实现不可变的结构类型,因为您无法禁用或覆盖赋值操作(=)。即使您定义了只读结构,您也可以随时更改

回答 1 投票 0

如何使两个相关字段根据odoo 18中的状态具有不同的只读行为?

我有这些相关领域 qty_request = fields.Float('请求数量') Product_qty = fields.Float('产品数量', digits = '产品计量单位', ...

回答 1 投票 0

用于只读数据库的java ORM

我了解 hibernate,但我想知道是否有一个更轻量级的 ORM 引擎用于只读数据库。我的意思是,我不需要一些事务查询或更新一些记录。另一方面,我需要

回答 8 投票 0

结构体上的只读方法有什么作用?

Visual Studio 建议我将结构体上的方法设置为只读,这是什么意思?我认为只有字段可以是只读的,而不是方法。 公共结构 MyStruct { ... // 我有

回答 1 投票 0

.Net 中编译器实现的不变性

鉴于这堂课... 公开课测试 { 私有长_id; 公开测试(长id) { _id = id; } } .Net 编译器实际上会将其编译为... 公开课测试 { 私人阅读...

回答 2 投票 0

Django + Crispy 表单 - 无需 Javascript 的特定用户组/用户的只读字段:可能吗?

我知道如何使用帮助器设置只读字段,但我想禁用特定用户的值更改。 我发现的唯一方法是在表单中添加一个条件,考虑到......

回答 1 投票 0

来自 XAML 中只读属性的 OneWayToSource 绑定

我正在尝试以 OneWayToSource 作为模式绑定到 Readonly 属性,但这似乎无法在 XAML 中完成: 我正在尝试以 Readonly 作为模式绑定到 OneWayToSource 属性,但似乎无法在 XAML 中完成: <controls:FlagThingy IsModified="{Binding FlagIsModified, ElementName=container, Mode=OneWayToSource}" /> 我得到: 无法设置属性“FlagThingy.IsModified”,因为它没有可访问的设置访问器。 IsModified 是 DependencyProperty 上的只读 FlagThingy。我想将该值绑定到容器上的 FlagIsModified 属性。 需要明确的是: FlagThingy.IsModified --> container.FlagIsModified ------ READONLY ----- ----- READWRITE -------- 仅使用 XAML 可以吗? 更新:好吧,我通过在容器上设置绑定而不是在FlagThingy上修复了这种情况。但我还是想知道这是否可能。 OneWayToSource 的一些研究成果... 选项#1。 // Control definition public partial class FlagThingy : UserControl { public static readonly DependencyProperty IsModifiedProperty = DependencyProperty.Register("IsModified", typeof(bool), typeof(FlagThingy), new PropertyMetadata()); } <controls:FlagThingy x:Name="_flagThingy" /> // Binding Code Binding binding = new Binding(); binding.Path = new PropertyPath("FlagIsModified"); binding.ElementName = "container"; binding.Mode = BindingMode.OneWayToSource; _flagThingy.SetBinding(FlagThingy.IsModifiedProperty, binding); 选项#2 // Control definition public partial class FlagThingy : UserControl { public static readonly DependencyProperty IsModifiedProperty = DependencyProperty.Register("IsModified", typeof(bool), typeof(FlagThingy), new PropertyMetadata()); public bool IsModified { get { return (bool)GetValue(IsModifiedProperty); } set { throw new Exception("An attempt ot modify Read-Only property"); } } } <controls:FlagThingy IsModified="{Binding Path=FlagIsModified, ElementName=container, Mode=OneWayToSource}" /> 选项#3(真正的只读依赖属性) System.ArgumentException:“IsModified”属性无法进行数据绑定。 // Control definition public partial class FlagThingy : UserControl { private static readonly DependencyPropertyKey IsModifiedKey = DependencyProperty.RegisterReadOnly("IsModified", typeof(bool), typeof(FlagThingy), new PropertyMetadata()); public static readonly DependencyProperty IsModifiedProperty = IsModifiedKey.DependencyProperty; } <controls:FlagThingy x:Name="_flagThingy" /> // Binding Code Same binding code... Reflector给出答案: internal static BindingExpression CreateBindingExpression(DependencyObject d, DependencyProperty dp, Binding binding, BindingExpressionBase parent) { FrameworkPropertyMetadata fwMetaData = dp.GetMetadata(d.DependencyObjectType) as FrameworkPropertyMetadata; if (((fwMetaData != null) && !fwMetaData.IsDataBindingAllowed) || dp.ReadOnly) { throw new ArgumentException(System.Windows.SR.Get(System.Windows.SRID.PropertyNotBindable, new object[] { dp.Name }), "dp"); } .... 这是 WPF 的限制,是设计使然。 Connect 上有报道: 来自只读依赖属性的 OneWayToSource 绑定 我制定了一个解决方案,可以动态地将只读依赖属性推送到名为 PushBinding 的源,我在 博客中提到了这里。下面的示例将 OneWayToSource 从只读 DP 的 ActualWidth 和 ActualHeight 绑定到 DataContext 的 Width 和 Height 属性 <TextBlock Name="myTextBlock"> <pb:PushBindingManager.PushBindings> <pb:PushBinding TargetProperty="ActualHeight" Path="Height"/> <pb:PushBinding TargetProperty="ActualWidth" Path="Width"/> </pb:PushBindingManager.PushBindings> </TextBlock> PushBinding 通过使用两个依赖属性(Listener 和 Mirror)来工作。侦听器已绑定 OneWay 到 TargetProperty,并在 PropertyChangedCallback 中更新 Mirror 属性,该属性已绑定 OneWayToSource 到 Binding 中指定的任何内容。 可以在这里下载演示项目。 它包含源代码和简短的示例用法。 写下这个: 用途: <TextBox Text="{Binding Text}" p:OneWayToSource.Bind="{p:Paths From={x:Static Validation.HasErrorProperty}, To=SomeDataContextProperty}" /> 代码: using System; using System.Windows; using System.Windows.Data; using System.Windows.Markup; public static class OneWayToSource { public static readonly DependencyProperty BindProperty = DependencyProperty.RegisterAttached( "Bind", typeof(ProxyBinding), typeof(OneWayToSource), new PropertyMetadata(default(Paths), OnBindChanged)); public static void SetBind(this UIElement element, ProxyBinding value) { element.SetValue(BindProperty, value); } [AttachedPropertyBrowsableForChildren(IncludeDescendants = false)] [AttachedPropertyBrowsableForType(typeof(UIElement))] public static ProxyBinding GetBind(this UIElement element) { return (ProxyBinding)element.GetValue(BindProperty); } private static void OnBindChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((ProxyBinding)e.OldValue)?.Dispose(); } public class ProxyBinding : DependencyObject, IDisposable { private static readonly DependencyProperty SourceProxyProperty = DependencyProperty.Register( "SourceProxy", typeof(object), typeof(ProxyBinding), new PropertyMetadata(default(object), OnSourceProxyChanged)); private static readonly DependencyProperty TargetProxyProperty = DependencyProperty.Register( "TargetProxy", typeof(object), typeof(ProxyBinding), new PropertyMetadata(default(object))); public ProxyBinding(DependencyObject source, DependencyProperty sourceProperty, string targetProperty) { var sourceBinding = new Binding { Path = new PropertyPath(sourceProperty), Source = source, Mode = BindingMode.OneWay, }; BindingOperations.SetBinding(this, SourceProxyProperty, sourceBinding); var targetBinding = new Binding() { Path = new PropertyPath($"{nameof(FrameworkElement.DataContext)}.{targetProperty}"), Mode = BindingMode.OneWayToSource, Source = source }; BindingOperations.SetBinding(this, TargetProxyProperty, targetBinding); } public void Dispose() { BindingOperations.ClearAllBindings(this); } private static void OnSourceProxyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { d.SetCurrentValue(TargetProxyProperty, e.NewValue); } } } [MarkupExtensionReturnType(typeof(OneWayToSource.ProxyBinding))] public class Paths : MarkupExtension { public DependencyProperty From { get; set; } public string To { get; set; } public override object ProvideValue(IServiceProvider serviceProvider) { var provideValueTarget = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)); var targetObject = (UIElement)provideValueTarget.TargetObject; return new OneWayToSource.ProxyBinding(targetObject, this.From, this.To); } } 尚未在样式和模板中进行测试,猜测它需要特殊的外壳。 这是另一个基于 SizeObserver 的附加属性解决方案,详细信息请参见此处 将只读 GUI 属性推回 ViewModel public static class MouseObserver { public static readonly DependencyProperty ObserveProperty = DependencyProperty.RegisterAttached( "Observe", typeof(bool), typeof(MouseObserver), new FrameworkPropertyMetadata(OnObserveChanged)); public static readonly DependencyProperty ObservedMouseOverProperty = DependencyProperty.RegisterAttached( "ObservedMouseOver", typeof(bool), typeof(MouseObserver)); public static bool GetObserve(FrameworkElement frameworkElement) { return (bool)frameworkElement.GetValue(ObserveProperty); } public static void SetObserve(FrameworkElement frameworkElement, bool observe) { frameworkElement.SetValue(ObserveProperty, observe); } public static bool GetObservedMouseOver(FrameworkElement frameworkElement) { return (bool)frameworkElement.GetValue(ObservedMouseOverProperty); } public static void SetObservedMouseOver(FrameworkElement frameworkElement, bool observedMouseOver) { frameworkElement.SetValue(ObservedMouseOverProperty, observedMouseOver); } private static void OnObserveChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { var frameworkElement = (FrameworkElement)dependencyObject; if ((bool)e.NewValue) { frameworkElement.MouseEnter += OnFrameworkElementMouseOverChanged; frameworkElement.MouseLeave += OnFrameworkElementMouseOverChanged; UpdateObservedMouseOverForFrameworkElement(frameworkElement); } else { frameworkElement.MouseEnter -= OnFrameworkElementMouseOverChanged; frameworkElement.MouseLeave -= OnFrameworkElementMouseOverChanged; } } private static void OnFrameworkElementMouseOverChanged(object sender, MouseEventArgs e) { UpdateObservedMouseOverForFrameworkElement((FrameworkElement)sender); } private static void UpdateObservedMouseOverForFrameworkElement(FrameworkElement frameworkElement) { frameworkElement.SetCurrentValue(ObservedMouseOverProperty, frameworkElement.IsMouseOver); } } 在控件中声明附加属性 <ListView ItemsSource="{Binding SomeGridItems}" ut:MouseObserver.Observe="True" ut:MouseObserver.ObservedMouseOver="{Binding IsMouseOverGrid, Mode=OneWayToSource}"> 这是绑定到 Validation.HasError 的另一个实现 public static class OneWayToSource { public static readonly DependencyProperty BindingsProperty = DependencyProperty.RegisterAttached( "Bindings", typeof(OneWayToSourceBindings), typeof(OneWayToSource), new PropertyMetadata(default(OneWayToSourceBindings), OnBinidngsChanged)); public static void SetBindings(this FrameworkElement element, OneWayToSourceBindings value) { element.SetValue(BindingsProperty, value); } [AttachedPropertyBrowsableForChildren(IncludeDescendants = false)] [AttachedPropertyBrowsableForType(typeof(FrameworkElement))] public static OneWayToSourceBindings GetBindings(this FrameworkElement element) { return (OneWayToSourceBindings)element.GetValue(BindingsProperty); } private static void OnBinidngsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((OneWayToSourceBindings)e.OldValue)?.ClearValue(OneWayToSourceBindings.ElementProperty); ((OneWayToSourceBindings)e.NewValue)?.SetValue(OneWayToSourceBindings.ElementProperty, d); } } public class OneWayToSourceBindings : FrameworkElement { private static readonly PropertyPath DataContextPath = new PropertyPath(nameof(DataContext)); private static readonly PropertyPath HasErrorPath = new PropertyPath($"({typeof(Validation).Name}.{Validation.HasErrorProperty.Name})"); public static readonly DependencyProperty HasErrorProperty = DependencyProperty.Register( nameof(HasError), typeof(bool), typeof(OneWayToSourceBindings), new FrameworkPropertyMetadata(default(bool), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); internal static readonly DependencyProperty ElementProperty = DependencyProperty.Register( "Element", typeof(UIElement), typeof(OneWayToSourceBindings), new PropertyMetadata(default(UIElement), OnElementChanged)); private static readonly DependencyProperty HasErrorProxyProperty = DependencyProperty.RegisterAttached( "HasErrorProxy", typeof(bool), typeof(OneWayToSourceBindings), new PropertyMetadata(default(bool), OnHasErrorProxyChanged)); public bool HasError { get { return (bool)this.GetValue(HasErrorProperty); } set { this.SetValue(HasErrorProperty, value); } } private static void OnHasErrorProxyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { d.SetCurrentValue(HasErrorProperty, e.NewValue); } private static void OnElementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (e.NewValue == null) { BindingOperations.ClearBinding(d, DataContextProperty); BindingOperations.ClearBinding(d, HasErrorProxyProperty); } else { var dataContextBinding = new Binding { Path = DataContextPath, Mode = BindingMode.OneWay, Source = e.NewValue }; BindingOperations.SetBinding(d, DataContextProperty, dataContextBinding); var hasErrorBinding = new Binding { Path = HasErrorPath, Mode = BindingMode.OneWay, Source = e.NewValue }; BindingOperations.SetBinding(d, HasErrorProxyProperty, hasErrorBinding); } } } xaml 中的用法 <StackPanel> <TextBox Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}"> <local:OneWayToSource.Bindings> <local:OneWayToSourceBindings HasError="{Binding HasError}" /> </local:OneWayToSource.Bindings> </TextBox> <CheckBox IsChecked="{Binding HasError, Mode=OneWay}" /> </StackPanel> 此实现特定于绑定 Validation.HasError WPF 不会使用 CLR 属性设置器,但似乎它会基于它进行一些奇怪的验证。 可能在你的情况下这可能没问题: public bool IsModified { get { return (bool)GetValue(IsModifiedProperty); } set { throw new Exception("An attempt ot modify Read-Only property"); } } 嗯...我不确定我是否同意这些解决方案。如何在属性注册中指定一个忽略外部更改的强制回调?例如,我需要实现一个只读 Position 依赖属性来获取 MediaElement 控件在用户控件内的位置。我是这样做的: public static readonly DependencyProperty PositionProperty = DependencyProperty.Register("Position", typeof(double), typeof(MediaViewer), new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal, OnPositionChanged, OnPositionCoerce)); private static void OnPositionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var ctrl = d as MediaViewer; } private static object OnPositionCoerce(DependencyObject d, object value) { var ctrl = d as MediaViewer; var position = ctrl.MediaRenderer.Position.TotalSeconds; if (ctrl.MediaRenderer.NaturalDuration.HasTimeSpan == false) return 0d; else return Math.Min(position, ctrl.Duration); } public double Position { get { return (double)GetValue(PositionProperty); } set { SetValue(PositionProperty, value); } } 换句话说,只需忽略更改并返回由不具有 public 修饰符的不同成员支持的值。 -- 在上面的例子中,MediaRenderer实际上是私有的MediaElement控件。 我解决此限制的方法是在类中仅公开 Binding 属性,将 DependencyProperty 完全保持为私有。我实现了一个“PropertyBindingToSource”只写属性(这个属性不是 DependencyProperty),它可以设置为 xaml 中的绑定值。在此只写属性的设置器中,我调用 BindingOperations.SetBinding 将绑定链接到 DependencyProperty。 对于OP的具体示例,它看起来像这样: FlatThingy 实现: public partial class FlatThingy : UserControl { public FlatThingy() { InitializeComponent(); } public Binding IsModifiedBindingToSource { set { if (value?.Mode != BindingMode.OneWayToSource) { throw new InvalidOperationException("IsModifiedBindingToSource must be set to a OneWayToSource binding"); } BindingOperations.SetBinding(this, IsModifiedProperty, value); } } public bool IsModified { get { return (bool)GetValue(IsModifiedProperty); } private set { SetValue(IsModifiedProperty, value); } } private static readonly DependencyProperty IsModifiedProperty = DependencyProperty.Register("IsModified", typeof(bool), typeof(FlatThingy), new PropertyMetadata(false)); private void Button_Click(object sender, RoutedEventArgs e) { IsModified = !IsModified; } } 请注意,静态只读 DependencyProperty 对象是私有的。在控件中,我添加了一个按钮,其单击由 Button_Click 处理。 在我的 window.xaml 中使用 FlatThingy 控件: <Window x:Class="ReadOnlyBinding.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:ReadOnlyBinding" mc:Ignorable="d" DataContext="{x:Static local:ViewModel.Instance}" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <TextBlock Text="{Binding FlagIsModified}" Grid.Row="0" /> <local:FlatThingy IsModifiedBindingToSource="{Binding FlagIsModified, Mode=OneWayToSource}" Grid.Row="1" /> </Grid> 请注意,我还实现了一个 ViewModel 用于绑定到此处未显示的视图模型。它公开了一个名为“FlagIsModified”的 DependencyProperty,您可以从上面的源代码中收集到。 它工作得很好,允许我以松散耦合的方式将信息从 View 推回 ViewModel,并明确定义信息流的方向。 您现在的装订方向错误。 每当您正在创建的控件上的 IsModified 发生更改时,OneWayToSource 将尝试更新容器上的 FlagIsModified。 您想要相反的结果,即将 IsModified 绑定到 container.FlagIsModified。 为此,您应该使用绑定模式 OneWay <controls:FlagThingy IsModified="{Binding FlagIsModified, ElementName=container, Mode=OneWay}" /> 枚举成员的完整列表:http://msdn.microsoft.com/en-us/library/system.windows.data.bindingmode.aspx

回答 9 投票 0

Python 类函数返回 super()

所以我正在摆弄一个只读可修改的类模式,这在java中很常见。它涉及创建一个包含只读属性的基类,并为修改扩展该类...

回答 1 投票 0

只读字段和私有 getter 属性之间的区别

我对只读字段和私有 getter 属性之间的区别感到非常困惑。我见过人们在代码中同时使用这两种方法,但我根本不明白是什么让它们有所不同。 私人

回答 1 投票 0

getter 与只读

以下有什么区别吗? C级 { // 一: 公共静态只读 int ValueAsAMmber = 42; // 二: 公共静态 int ValueAsAProperty { 获取 { 返回 42; } } }...

回答 9 投票 0

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.