UWP C# 中的 XamlCompositionBrushBase 问题
我用 C# 制作了一个丙烯酸画笔克隆。当我在运行应用程序之前禁用节电模式来运行它时,它工作得很好,但是当我启用节电模式并再次运行它时,当我在应用程序运行时禁用节电模式时,它不会启用透明度。 Windows 中“设置”菜单中的透明度切换也是如此。那么,当运行应用程序时禁用省电模式(例如)时,有什么方法可以重新加载画笔呢?
这是代码:
bool isWindowed = BackgroundSource == CustomAcrylicBackgroundSource.Hostbackdrop;
if (useFallback == false && (DefaultTheme.AdvancedEffectsEnabled && PowerManager.EnergySaverStatus != EnergySaverStatus.On)
&& !(UIViewSettings.GetForCurrentView().UserInteractionMode == UserInteractionMode.Touch && isWindowed))
{
ArithmeticCompositeEffect crossFadeEffect = new ArithmeticCompositeEffect
{
MultiplyAmount = 0f,
Source1Amount = (float)TintOpacity,
Source2Amount = 1 - (float)TintOpacity,
Source1 = new ColorSourceEffect
{
Color = TintColor
},
Source2 = new ArithmeticCompositeEffect
{
MultiplyAmount = 0f,
Source1Amount = sc_noiseOpacity,
Source2Amount = 0.98f,
Source1 = new BorderEffect
{
Source = new CompositionEffectSourceParameter("image"),
ExtendX = CanvasEdgeBehavior.Wrap,
ExtendY = CanvasEdgeBehavior.Wrap,
},
Source2 = new BlendEffect
{
Mode = BlendEffectMode.Exclusion,
Foreground = new ColorSourceEffect
{
Color = Sc_exclusionColor
},
Background = new SaturationEffect
{
Saturation = sc_saturation,
Source = new GaussianBlurEffect
{
BlurAmount = sc_blurRadius,
BorderMode = EffectBorderMode.Hard,
Name = "Blur",
Optimization = EffectOptimization.Balanced,
Source = new CompositionEffectSourceParameter("source")
}
}
}
}
};
CompositionEffectFactory effectFactory = compositor.CreateEffectFactory(crossFadeEffect);
effectBrush = effectFactory.CreateBrush();
LoadedImageSurface imagesurface = LoadedImageSurface.StartLoadFromUri(new Uri("ms-appx:///UnitedCodebase/Assets/NoiseAsset_256X256_PNG.png"));
CompositionSurfaceBrush imagebrush = compositor.CreateSurfaceBrush(imagesurface);
imagebrush.Stretch = CompositionStretch.None;
effectBrush.SetSourceParameter("image", imagebrush);
if (BackgroundSource == CustomAcrylicBackgroundSource.Hostbackdrop)
{
backdropBrush = compositor.CreateHostBackdropBrush();
}
else
{
backdropBrush = compositor.CreateBackdropBrush();
}
effectBrush.SetSourceParameter("source", backdropBrush);
CompositionBrush = effectBrush;
}
else
{
CompositionBrush = compositor.CreateColorBrush(FallbackColor);
}
private void DisconnectAcryilicBrush()
{
if (backdropBrush != null)
{
backdropBrush.Dispose();
backdropBrush = null;
}
if (CompositionBrush != null)
{
CompositionBrush.Dispose();
CompositionBrush = null;
}
}
用法:
protected override void OnConnected()
{
bool usingFallback;
if (BackgroundSource == CustomAcrylicBackgroundSource.Hostbackdrop)
{
usingFallback = !CompositionCapabilities.GetForCurrentView().AreEffectsSupported();
}
else
{
usingFallback = !CompositionCapabilities.GetForCurrentView().AreEffectsFast();
}
ConnectAcrylicBrush(usingFallback);
base.OnConnected();
}
protected override void OnDisconnected()
{
DisconnectAcryilicBrush();
base.OnDisconnected();
}
I've made a acrylic brush clone in C#. It works perfectly when I've running it with battery saver is disabled before running app, but when I'm enabling the battery saver and running it again, it does not enabling transparency when I'm disabling the battery saver when app is running. The same is with transparency toggle in Settings menu in Windows. So there is any way to reload the brush when battery saver is disabled (for example) in running app?
Here is the code:
bool isWindowed = BackgroundSource == CustomAcrylicBackgroundSource.Hostbackdrop;
if (useFallback == false && (DefaultTheme.AdvancedEffectsEnabled && PowerManager.EnergySaverStatus != EnergySaverStatus.On)
&& !(UIViewSettings.GetForCurrentView().UserInteractionMode == UserInteractionMode.Touch && isWindowed))
{
ArithmeticCompositeEffect crossFadeEffect = new ArithmeticCompositeEffect
{
MultiplyAmount = 0f,
Source1Amount = (float)TintOpacity,
Source2Amount = 1 - (float)TintOpacity,
Source1 = new ColorSourceEffect
{
Color = TintColor
},
Source2 = new ArithmeticCompositeEffect
{
MultiplyAmount = 0f,
Source1Amount = sc_noiseOpacity,
Source2Amount = 0.98f,
Source1 = new BorderEffect
{
Source = new CompositionEffectSourceParameter("image"),
ExtendX = CanvasEdgeBehavior.Wrap,
ExtendY = CanvasEdgeBehavior.Wrap,
},
Source2 = new BlendEffect
{
Mode = BlendEffectMode.Exclusion,
Foreground = new ColorSourceEffect
{
Color = Sc_exclusionColor
},
Background = new SaturationEffect
{
Saturation = sc_saturation,
Source = new GaussianBlurEffect
{
BlurAmount = sc_blurRadius,
BorderMode = EffectBorderMode.Hard,
Name = "Blur",
Optimization = EffectOptimization.Balanced,
Source = new CompositionEffectSourceParameter("source")
}
}
}
}
};
CompositionEffectFactory effectFactory = compositor.CreateEffectFactory(crossFadeEffect);
effectBrush = effectFactory.CreateBrush();
LoadedImageSurface imagesurface = LoadedImageSurface.StartLoadFromUri(new Uri("ms-appx:///UnitedCodebase/Assets/NoiseAsset_256X256_PNG.png"));
CompositionSurfaceBrush imagebrush = compositor.CreateSurfaceBrush(imagesurface);
imagebrush.Stretch = CompositionStretch.None;
effectBrush.SetSourceParameter("image", imagebrush);
if (BackgroundSource == CustomAcrylicBackgroundSource.Hostbackdrop)
{
backdropBrush = compositor.CreateHostBackdropBrush();
}
else
{
backdropBrush = compositor.CreateBackdropBrush();
}
effectBrush.SetSourceParameter("source", backdropBrush);
CompositionBrush = effectBrush;
}
else
{
CompositionBrush = compositor.CreateColorBrush(FallbackColor);
}
private void DisconnectAcryilicBrush()
{
if (backdropBrush != null)
{
backdropBrush.Dispose();
backdropBrush = null;
}
if (CompositionBrush != null)
{
CompositionBrush.Dispose();
CompositionBrush = null;
}
}
Usage:
protected override void OnConnected()
{
bool usingFallback;
if (BackgroundSource == CustomAcrylicBackgroundSource.Hostbackdrop)
{
usingFallback = !CompositionCapabilities.GetForCurrentView().AreEffectsSupported();
}
else
{
usingFallback = !CompositionCapabilities.GetForCurrentView().AreEffectsFast();
}
ConnectAcrylicBrush(usingFallback);
base.OnConnected();
}
protected override void OnDisconnected()
{
DisconnectAcryilicBrush();
base.OnDisconnected();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不得不说这是不可能的。透明效果由系统控制。当从“设置”页面禁用透明效果功能时,所有透明效果将不起作用。
本文档中也提到了这一点:亚克力材质。从红色注释中可以看出,渲染亚克力表面需要 GPU 密集型操作,这会增加设备功耗并缩短电池寿命。 当设备进入省电模式时,亚克力效果会自动禁用。用户可以通过在“设置”>“透明效果”中关闭透明效果来禁用所有应用程序的丙烯酸效果。个性化>颜色。
I have to say that is not possible. The transparency effect is controlled by the system. When the transparency effect feature is disabled from the Settings page, all the transparency effects will not work.
This is also mentioned in this document: Acrylic material. From the red note, it says Rendering acrylic surfaces is GPU-intensive, which can increase device power consumption and shorten battery life. Acrylic effects are automatically disabled when a device enters Battery Saver mode. Users can disable acrylic effects for all apps by turning off Transparency effects in Settings > Personalization > Colors.