Browse Source
* Avoid hardcoding strings in DateTimePicker cs files * Add invariant resources * Implement ResourceProvider for flexibility of localization with custom resource provider * Fix these weird tests * Seal some ResourceDictionary extension points * Replace "Locale" with "String"pull/14744/head
committed by
GitHub
21 changed files with 268 additions and 145 deletions
@ -0,0 +1,102 @@ |
|||
using System; |
|||
using Avalonia.Styling; |
|||
|
|||
namespace Avalonia.Controls; |
|||
|
|||
/// <summary>
|
|||
/// Base implementation for IResourceProvider interface.
|
|||
/// Includes Owner property management.
|
|||
/// </summary>
|
|||
public abstract class ResourceProvider : IResourceProvider |
|||
{ |
|||
private IResourceHost? _owner; |
|||
|
|||
public ResourceProvider() |
|||
{ |
|||
} |
|||
|
|||
public ResourceProvider(IResourceHost owner) |
|||
{ |
|||
_owner = owner; |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public abstract bool HasResources { get; } |
|||
|
|||
/// <inheritdoc/>
|
|||
public abstract bool TryGetResource(object key, ThemeVariant? theme, out object? value); |
|||
|
|||
/// <inheritdoc/>
|
|||
public IResourceHost? Owner |
|||
{ |
|||
get => _owner; |
|||
private set |
|||
{ |
|||
if (_owner != value) |
|||
{ |
|||
_owner = value; |
|||
OwnerChanged?.Invoke(this, EventArgs.Empty); |
|||
} |
|||
} |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public event EventHandler? OwnerChanged; |
|||
|
|||
protected void RaiseResourcesChanged() |
|||
{ |
|||
Owner?.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Handles when owner was added.
|
|||
/// Base method implementation raises <see cref="IResourceHost.NotifyHostedResourcesChanged"/>, if this provider has any resources.
|
|||
/// </summary>
|
|||
/// <param name="owner">New owner.</param>
|
|||
protected virtual void OnAddOwner(IResourceHost owner) |
|||
{ |
|||
if (HasResources) |
|||
{ |
|||
owner.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Handles when owner was removed.
|
|||
/// Base method implementation raises <see cref="IResourceHost.NotifyHostedResourcesChanged"/>, if this provider has any resources.
|
|||
/// </summary>
|
|||
/// <param name="owner">Old owner.</param>
|
|||
protected virtual void OnRemoveOwner(IResourceHost owner) |
|||
{ |
|||
if (HasResources) |
|||
{ |
|||
owner.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty); |
|||
} |
|||
} |
|||
|
|||
void IResourceProvider.AddOwner(IResourceHost owner) |
|||
{ |
|||
owner = owner ?? throw new ArgumentNullException(nameof(owner)); |
|||
|
|||
if (Owner != null) |
|||
{ |
|||
throw new InvalidOperationException("The ResourceDictionary already has a parent."); |
|||
} |
|||
|
|||
Owner = owner; |
|||
|
|||
OnAddOwner(owner); |
|||
} |
|||
|
|||
void IResourceProvider.RemoveOwner(IResourceHost owner) |
|||
{ |
|||
owner = owner ?? throw new ArgumentNullException(nameof(owner)); |
|||
|
|||
if (Owner == owner) |
|||
{ |
|||
Owner = null; |
|||
|
|||
OnRemoveOwner(owner); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
<ResourceDictionary xmlns="https://github.com/avaloniaui" |
|||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
|||
x:ClassModifier="internal"> |
|||
<!-- DatePicker --> |
|||
<x:String x:Key="StringDatePickerDayText">day</x:String> |
|||
<x:String x:Key="StringDatePickerMonthText">month</x:String> |
|||
<x:String x:Key="StringDatePickerYearText">year</x:String> |
|||
<!-- TimePicker --> |
|||
<x:String x:Key="StringTimePickerHourText">hour</x:String> |
|||
<x:String x:Key="StringTimePickerMinuteText">minute</x:String> |
|||
<!-- TextBox/SelectableTextBox flyout --> |
|||
<x:String x:Key="StringTextFlyoutCutText">Cut</x:String> |
|||
<x:String x:Key="StringTextFlyoutCopyText">Copy</x:String> |
|||
<x:String x:Key="StringTextFlyoutPasteText">Paste</x:String> |
|||
<!-- ManagedFileChooser --> |
|||
<x:String x:Key="StringManagedFileChooserFileNameWatermark">File name</x:String> |
|||
<x:String x:Key="StringManagedFileChooserShowHiddenFilesText">Show hidden files</x:String> |
|||
<x:String x:Key="StringManagedFileChooserOkText">OK</x:String> |
|||
<x:String x:Key="StringManagedFileChooserCancelText">Cancel</x:String> |
|||
<x:String x:Key="StringManagedFileChooserNameColumn">Name</x:String> |
|||
<x:String x:Key="StringManagedFileChooserDateModifiedColumn">Date Modified</x:String> |
|||
<x:String x:Key="StringManagedFileChooserTypeColumn">Type</x:String> |
|||
<x:String x:Key="StringManagedFileChooserSizeColumn">Size</x:String> |
|||
<x:String x:Key="StringManagedFileChooserOverwritePromptFileAlreadyExistsText">{0} already exists. Do you want to replace it?</x:String> |
|||
<x:String x:Key="StringManagedFileChooserOverwritePromptConfirmText">Yes</x:String> |
|||
<x:String x:Key="StringManagedFileChooserOverwritePromptCancelText">No</x:String> |
|||
</ResourceDictionary> |
|||
|
|||
Loading…
Reference in new issue