简体   繁体   English

将 IInspectable 转换为 RenderingEventArgs

[英]Converting an IInspectable to a RenderingEventArgs

How do you convert an IInspectable reference to a RenderingEventArgs in the following taken from the full xaml_app code below?在下面的完整 xaml_app 代码中,如何将 IInspectable 引用转换为 RenderingEventArgs?

CompositionTarget::Rendering(EventHandler::new(move |_, args: &Option<IInspectable>| {
        let args: &IInspectable = args.as_ref().unwrap();
            //let render_args = RenderingEventArgs::from(&args);
            //let render_args: RenderingEventArgs = args.try_into()?;

            Ok(())
        }))?;

Full code listing follows for context:上下文的完整代码清单如下:

#![windows_subsystem = "windows"]
#![allow(non_snake_case)]

use windows::{
    core::{
        IInspectable,
        Result,
        HSTRING,
        implement,
    },
    ApplicationModel::Activation::LaunchActivatedEventArgs,
    Win32::System::Com::{
        CoInitializeEx,
        COINIT_MULTITHREADED,
    },
    Foundation::EventHandler,
    //Foundation::TypedEventHandler,
    UI::{
        Colors,
        Xaml::{
            Controls::{
                TextBlock,
            },
            Application,
            ApplicationInitializationCallback,
            Window,
            Media::{
                FontFamily,
                SolidColorBrush,
                CompositionTarget,
                RenderingEventArgs,
            },
            HorizontalAlignment,
            VerticalAlignment,
            FrameworkElement,
            IApplicationOverrides,
            IApplicationOverrides_Impl,
        },
    },
};
//use std::convert::TryInto;

#[implement(IApplicationOverrides)]
struct App();

impl IApplicationOverrides_Impl for App {
    fn OnLaunched(&self, _: &Option<LaunchActivatedEventArgs>) -> Result<()> {
        let block = TextBlock::new()?;

        block.SetFontFamily(FontFamily::CreateInstanceWithName(HSTRING::from("Segoe UI Semibold"))?)?;
        block.SetFontSize(72.0)?;
        block.SetForeground(SolidColorBrush::CreateInstanceWithColor(Colors::Orange()?)?)?;

        let fe = FrameworkElement::from(&block);
        fe.SetHorizontalAlignment(HorizontalAlignment::Center)?;
        fe.SetVerticalAlignment(VerticalAlignment::Center)?;
        
        let window = Window::Current()?;
        window.SetContent(&block)?;
        window.Activate()?;

        CompositionTarget::Rendering(EventHandler::new(move |_, args: &Option<IInspectable>| {
        let args: &IInspectable = args.as_ref().unwrap();
            //let render_args = RenderingEventArgs::from(&args);
            //let render_args: RenderingEventArgs = args.try_into()?;

            Ok(())
        }))?;

        Ok(())
    }
}

fn main() -> Result<()> {
    unsafe {
        CoInitializeEx(std::ptr::null(), COINIT_MULTITHREADED)?;
    }
    Application::Start(ApplicationInitializationCallback::new(|_| {
        Application::compose(App())?;

        Ok(())
    }))
}

[package]
name = "expanding_text"
version = "0.0.0"
edition = "2018"

[dependencies]
futures = "0.3"

[dependencies.windows]
version = "0.36"
features = [
    "implement",
    "ApplicationModel_Activation",
    "Win32_System_Com",
    "UI_Xaml_Controls",
    "UI_Xaml",
    "UI_Xaml_Media",
    "UI",
    "Storage_Pickers",
    "Foundation_Collections",
    "Storage",
    "Foundation",
    "Storage_Streams",
    "Graphics_Imaging",
    "Media_Ocr",
    "ApplicationModel_Core",
    "UI_Core",
]

The following line appears to make the conversion and builds:以下行似乎进行了转换和构建:

let render_args: RenderingEventArgs = args.cast()?;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM