21、WindowProperties

WindowProperties项目展示了如何在WPF应用中利用属性显示窗口的大小,并在窗口尺寸改变时自动更新显示。通过创建项目、设计界面、修改代码及运行测试,实现窗口尺寸与文本框内容同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WindowProperties

该项目展示了一个简单的Windows应用程序,该程序使用几个属性(特性)来显示其主窗口的大小。显示内容随用户重设窗口大小而自动更新。

(1) 创建项目

创建WPF Application项目:WindowProperties

(2) 设计界面

 

<Window x:Class="WindowProperties.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Window Properties" Height="150" Width="300" SizeChanged="Window_SizeChanged">

    <Grid>

        <Label Content="Width" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Width="82" />

        <Label Content="Height" Height="28" HorizontalAlignment="Left" Margin="184,12,0,0" VerticalAlignment="Top" Width="82" />

        <TextBox Height="23" HorizontalAlignment="Left" Margin="12,58,0,0" Name="textBoxWidth" VerticalAlignment="Top" Width="82" />

        <TextBox Height="23" HorizontalAlignment="Left" Margin="184,58,0,0" Name="textBoxHeight" VerticalAlignment="Top" Width="82" />

    </Grid>

</Window>

(3) 修改MainWindow.xaml.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

 

namespace WindowProperties

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

            sizeChanged();

        }

 

        private void sizeChanged()

        {

            textBoxWidth.Text = this.ActualWidth.ToString();

            textBoxHeight.Text = this.ActualHeight.ToString();

        }

 

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)

        {

            sizeChanged();

        }

    }

}

(4) 运行测试

 

 

资源下载

android / platform / frameworks / support / 4d53400eca9f3ac90c3a3f6cffcbc5bf492ec536 / . / window / window / src / main / java / androidx / window / embedding / RuleController.kt blob: 192f3ecb19e01e80afab7ef3ec7fc0c21b2bea64 [file] [log] [blame] /* * Copyright 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package androidx.window.embedding import android.content.Context import androidx.annotation.XmlRes import androidx.window.embedding.RuleController.Companion.parseRules /** * The controller to manage [EmbeddingRule]s. It supports: * - [addRule] * - [removeRule] * - [setRules] * - [parseRules] * - [clearRules] * * **Note** that this class is recommended to be configured in [androidx.startup.Initializer] or * [android.app.Application.onCreate], so that the rules are applied early in the application * startup before any activities complete initialization. The rule updates only apply to future * [android.app.Activity] launches and do not apply to already running activities. */ class RuleController internal constructor(private val embeddingBackend: EmbeddingBackend) { // TODO(b/258356512): Make this API a make this a coroutine API that returns // Flow<Set<EmbeddingRule>>. /** * Returns a copy of the currently registered rules. */ fun getRules(): Set<EmbeddingRule> { return embeddingBackend.getRules() } /** * Registers a new rule, or updates an existing rule if the [tag][EmbeddingRule.tag] has been * registered with [RuleController]. Will be cleared automatically when the process is stopped. * * Registering a `SplitRule` may fail if the [SplitController.splitSupportStatus] * returns `false`. If not supported, it could be either because * [androidx.window.WindowProperties.PROPERTY_ACTIVITY_EMBEDDING_SPLITS_ENABLED] not enabled * in AndroidManifest or the feature not available on the device. * * Note that registering a new rule or updating the existing rule will **not** be applied to any * existing split activity container, and will only be used for new split containers created * with future activity launches. * * @param rule new [EmbeddingRule] to register. */ fun addRule(rule: EmbeddingRule) { embeddingBackend.addRule(rule) } /** * Unregisters a rule that was previously registered via [addRule] or [setRules]. * * @param rule the previously registered [EmbeddingRule] to unregister. */ fun removeRule(rule: EmbeddingRule) { embeddingBackend.removeRule(rule) } /** * Sets a set of [EmbeddingRule]s, which replace all rules registered by [addRule] * or [setRules]. * * It's recommended to set the rules via an [androidx.startup.Initializer], or * [android.app.Application.onCreate], so that they are applied early in the application * startup before any activities appear. * * The [EmbeddingRule]s can be parsed from [parseRules] or built with rule Builders, which are: * - [SplitPairRule.Builder] * - [SplitPlaceholderRule.Builder] * - [ActivityRule.Builder] * * Registering `SplitRule`s may fail if the [SplitController.splitSupportStatus] * returns `false`. If not supported, it could be either because * [androidx.window.WindowProperties.PROPERTY_ACTIVITY_EMBEDDING_SPLITS_ENABLED] not enabled * in AndroidManifest or the feature not available on the device. * * Note that updating the existing rules will **not** be applied to any existing split activity * container, and will only be used for new split containers created with future activity * launches. * * @param rules The [EmbeddingRule]s to set * @throws IllegalArgumentException if [rules] contains two [EmbeddingRule]s with the same * [EmbeddingRule.tag]. */ fun setRules(rules: Set<EmbeddingRule>) { embeddingBackend.setRules(rules) } /** Clears the rules previously registered by [addRule] or [setRules]. */ fun clearRules() { embeddingBackend.setRules(emptySet()) } companion object { /** * Obtains an instance of [RuleController]. * * @param context the [Context] to initialize the controller with */ @JvmStatic fun getInstance(context: Context): RuleController { val applicationContext = context.applicationContext val backend = EmbeddingBackend.getInstance(applicationContext) return RuleController(backend) } /** * Parses [EmbeddingRule]s from XML rule definitions. * * The [EmbeddingRule]s can then set by [setRules]. * * @param context the context that contains the XML rule definition resources * @param staticRuleResourceId the resource containing the static split rules. * @throws IllegalArgumentException if any of the rules in the XML are malformed. */ @JvmStatic fun parseRules(context: Context, @XmlRes staticRuleResourceId: Int): Set<EmbeddingRule> = RuleParser.parseRules(context.applicationContext, staticRuleResourceId) ?: emptySet() } } 将这个kotlin文件转为java文件
最新发布
08-12
<think>我们将Kotlin文件RuleController.kt转换为Java文件。注意以下几点: 1. Kotlin的伴生对象(companion object)转换为Java的静态方法和静态内部类(如果需要)。 2. Kotlin的set和list转换为Java的Set和List。 3. 处理可空性和注解。 4. 将Kotlin的默认可见性(public)转换为Java的public。 由于RuleController的构造器是internal的,我们在Java中将其设为包私有(无修饰符)并提供一个静态工厂方法getInstance。 同时,我们需要注意伴生对象中的两个静态方法:getInstance和parseRules。 另外,RuleController中有一个companion object,其中的方法在Java中会以静态方法的形式出现。 我们假设EmbeddingBackend和EmbeddingRule等类已经存在,并且我们按照Kotlin的语义进行转换。 注意:在Kotlin中,RuleController的构造器是internal的,但在Java中我们无法直接使用internal,因此我们将其设为包私有(即不使用public、protected或private修饰符),并确保只有同包下的类(如EmbeddingBackend)可以构造它。但是,由于我们是通过getInstance方法获取实例,所以实际上在Java中我们通过静态工厂方法获取。 但是,由于RuleController和EmbeddingBackend可能不在同一个包中,我们可能需要调整访问权限。这里我们按照原Kotlin文件的意图,将RuleController的构造器设为public(因为Kotlin的internal在跨模块时不可见,而在同一个模块中,Java代码无法直接使用internal,所以这里我们假设RuleController和EmbeddingBackend在同一个包中,并且我们使用包私有构造器。但为了安全起见,我们通常将构造器设为public,因为Kotlin的internal在Java中没有直接对应,而且我们是在同一个模块中转换)。 然而,根据原始Kotlin代码,RuleController的构造器是internal的,这意味着它只能在同一个模块中访问。在Java中,我们可以通过将构造器设为public来模拟(因为Java没有模块可见性),但这样会扩大访问权限。另一种做法是使用包私有,但这样在另一个包中就无法访问。所以,我们这里按照Kotlin的意图,将构造器设为public,因为RuleController是通过getInstance方法获取的,而getInstance方法在同一个模块中,所以可以访问构造器。 但是,请注意:在Kotlin中,internal修饰符表示模块内可见,而Java中我们无法直接模拟,所以通常做法是使用public,然后通过文档说明。 然而,我们注意到RuleController的构造器参数是EmbeddingBackend,而EmbeddingBackend.getInstance返回的是EmbeddingBackend实例。我们假设EmbeddingBackend在同一个模块中,并且RuleController的构造器在Java中设为public,这样getInstance方法就可以创建RuleController实例。 但是,为了更符合Kotlin的internal,我们可以将RuleController的构造器设为包私有(即去掉public),并让RuleController和EmbeddingBackend在同一个包中。这样,只有同一个包下的类(如EmbeddingBackend)才能创建RuleController实例。但是,我们的getInstance方法在同一个类中,所以可以访问包私有构造器。 因此,我们决定将RuleController的构造器设为包私有(即不使用任何访问修饰符,这样在同一个包内可见)。但是,由于getInstance是RuleController的静态方法,它可以访问包私有构造器。 然而,在Java中,同一个包下的类可以访问包私有成员。所以,我们这样设计: 我们将RuleController的构造器设为包私有(即不使用public、private、protected),这样在同一个包下的其他类也可以访问。但是,由于我们是通过getInstance方法获取实例,所以外部代码应该使用getInstance,而不是直接调用构造器。 但是,为了确保外部代码不能直接调用构造器,我们也可以将构造器设为private,然后让getInstance方法使用它。因为getInstance是同一个类的方法,所以可以访问private构造器。 所以,我们选择将构造器设为private,这样外部代码无法直接创建RuleController实例,只能通过getInstance方法获取。 因此,我们转换如下: 1. 将RuleController类转换为Java类。 2. 构造器为private,参数为EmbeddingBackend。 3. 提供静态方法getInstance。 4. 将companion object中的方法转换为静态方法。 另外,parseRules方法在Kotlin中调用了RuleParser.parseRules,我们同样需要转换RuleParser,但这里我们只转换RuleController。 注意:parseRules方法在Kotlin中可能返回null,所以我们在Java中返回一个Set<EmbeddingRule>,如果RuleParser.parseRules返回null,则返回空集合。 由于RuleParser.parseRules在Kotlin中可能返回null,我们在Java中做同样的处理。 但是,RuleParser我们还没有转换,所以这里我们假设RuleParser有一个对应的Java方法。 我们按照上述思路转换。</think>以下是将 Kotlin 文件 `RuleController.kt` 转换为 Java 文件的完整实现: ```java import android.content.Context; import androidx.annotation.XmlRes; import androidx.window.embedding.EmbeddingBackend; import androidx.window.embedding.EmbeddingRule; import java.util.Collections; import java.util.Set; public class RuleController { private final EmbeddingBackend embeddingBackend; private RuleController(EmbeddingBackend embeddingBackend) { this.embeddingBackend = embeddingBackend; } /** * Returns a copy of the currently registered rules. */ @NonNull public Set<EmbeddingRule> getRules() { return embeddingBackend.getRules(); } /** * Registers a new rule, or updates an existing rule. * @param rule new {@link EmbeddingRule} to register. */ public void addRule(@NonNull EmbeddingRule rule) { embeddingBackend.addRule(rule); } /** * Unregisters a previously registered rule. * @param rule the previously registered {@link EmbeddingRule} to unregister. */ public void removeRule(@NonNull EmbeddingRule rule) { embeddingBackend.removeRule(rule); } /** * Sets a set of {@link EmbeddingRule}s, replacing all existing rules. * @param rules The {@link EmbeddingRule}s to set * @throws IllegalArgumentException if {@code rules} contains two rules with the same tag. */ public void setRules(@NonNull Set<EmbeddingRule> rules) { embeddingBackend.setRules(rules); } /** Clears all registered rules. */ public void clearRules() { embeddingBackend.setRules(Collections.emptySet()); } /** * Obtains an instance of {@link RuleController}. * @param context the {@link Context} to initialize the controller with */ @NonNull public static RuleController getInstance(@NonNull Context context) { Context applicationContext = context.getApplicationContext(); EmbeddingBackend backend = EmbeddingBackend.getInstance(applicationContext); return new RuleController(backend); } /** * Parses {@link EmbeddingRule}s from XML rule definitions. * @param context the context that contains the XML rule definition resources * @param staticRuleResourceId the resource containing the static split rules. * @throws IllegalArgumentException if any of the rules in the XML are malformed. */ @NonNull public static Set<EmbeddingRule> parseRules( @NonNull Context context, @XmlRes int staticRuleResourceId ) { Context appContext = context.getApplicationContext(); Set<EmbeddingRule> rules = RuleParser.parseRules(appContext, staticRuleResourceId); return rules != null ? rules : Collections.emptySet(); } } ``` ### 关键转换说明: 1. **类结构转换**: - Kotlin 类转换为 Java 类 - 私有构造方法确保只能通过 `getInstance()` 获取实例 - 使用 `@NonNull` 注解明确非空约束 2. **方法转换**: - Kotlin 扩展函数转换为静态方法 - 伴生对象方法转换为静态方法 - 空安全处理使用三元运算符替代 Elvis 操作符 3. **集合处理**: - Kotlin 的 `emptySet()` 转换为 `Collections.emptySet()` - 返回不可修改的集合副本确保安全性 - 使用泛型保持类型安全 4. **依赖管理**: - 通过 `EmbeddingBackend.getInstance()` 获取后端实现 - 委托所有操作到嵌入后端 5. **资源处理**: - 使用应用上下文避免内存泄漏 - `@XmlRes` 注解确保资源类型正确 ### 需要补充的依赖类: 1. **`RuleParser` 辅助类**: ```java import android.content.Context; import androidx.annotation.XmlRes; import java.util.Set; public class RuleParser { public static Set<EmbeddingRule> parseRules(Context context, @XmlRes int resourceId) { // 实际解析 XML 资源的实现 // 这里返回空集表示占位实现 return Collections.emptySet(); } } ``` 2. **`EmbeddingBackend` 接口**: ```java import java.util.Set; public interface EmbeddingBackend { Set<EmbeddingRule> getRules(); void addRule(EmbeddingRule rule); void removeRule(EmbeddingRule rule); void setRules(Set<EmbeddingRule> rules); static EmbeddingBackend getInstance(Context context) { // 返回实际的后端实现 return new EmbeddingBackendImpl(context); } // 示例实现 class EmbeddingBackendImpl implements EmbeddingBackend { private final Set<EmbeddingRule> rules = new CopyOnWriteArraySet<>(); EmbeddingBackendImpl(Context context) { // 初始化逻辑 } @Override public Set<EmbeddingRule> getRules() { return Collections.unmodifiableSet(rules); } @Override public void addRule(EmbeddingRule rule) { rules.add(rule); } @Override public void removeRule(EmbeddingRule rule) { rules.remove(rule); } @Override public void setRules(Set<EmbeddingRule> rules) { this.rules.clear(); this.rules.addAll(rules); } } } ``` ### 使用示例: ```java // 在 Application 类中初始化 public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); RuleController controller = RuleController.getInstance(this); // 添加规则 SplitPairRule rule = new SplitPairRule.Builder(...).build(); controller.addRule(rule); // 或者一次性设置所有规则 Set<EmbeddingRule> rules = new HashSet<>(); rules.add(rule); controller.setRules(rules); // 或者从 XML 资源加载规则 Set<EmbeddingRule> parsedRules = RuleController.parseRules(this, R.xml.embedding_rules); controller.setRules(parsedRules); } } ``` ### 设计要点: 1. **单一职责**: - `RuleController` 仅负责规则管理 - 解析逻辑委托给 `RuleParser` - 后端实现委托给 `EmbeddingBackend` 2. **不可变性**: - `getRules()` 返回不可修改的集合 - 防止外部修改内部状态 3. **线程安全**: - 后端实现应处理并发访问 - 使用 `CopyOnWriteArraySet` 等并发集合 4. **生命周期管理**: - 使用应用上下文避免内存泄漏 - 规则在进程停止时自动清除
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值