我正在制作一个xamarin.form应用程序, 我有一个自定义渲染器,用于在视图中流式传输相机,我在这里遵循了教程:
我设法在视图中正确显示摄像机流,现在我试图创建一个用于切换闪光灯的按钮:
这是我的自定义渲染器:
using System;
using System.Diagnostics;
using Hangover.Camera.Factory;
using Hangover.GUIPersonalizzata;
using Xamarin.Forms;
//Questa classe permette di visualizzare lo stream della fotocamera
namespace Hangover.Camera.Views
{
public class CameraStream:View
{
public static readonly BindableProperty CameraProperty = BindableProperty.Create(
propertyName: "Camera",
returnType: typeof(CameraOptions),
declaringType: typeof(CameraStream),
defaultValue: CameraOptions.Rear);
//Funzioni Fotocamera
private Fotocamera _fotocamera;
public event EventHandler flash_event;
//costruttore di convenienza
public CameraStream()
{
}
public CameraStream(Fotocamera fotocamera){
_fotocamera = fotocamera;
}
public CameraOptions Camera
{
get { return (CameraOptions)GetValue(CameraProperty); }
set { SetValue(CameraProperty, value); }
}
public void gestisciFlash(BottoneSwitch btn){
if (flash_event != null)
flash_event.Invoke(btn, null);
}
}
}
这是iOS实现:
using System;
using System.Diagnostics;
using Hangover.Camera.Factory;
using Hangover.Camera.Views;
using Hangover.GUIPersonalizzata;
using Hangover.iOS.CustomRenderer;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(CameraStream), typeof(CameraStreamRenderer))]
namespace Hangover.iOS.CustomRenderer
{
public class CameraStreamRenderer : ViewRenderer<CameraStream, UICameraPreview>,ICameraFunctions
{
UICameraPreview uiCameraPreview; // fotocamera nativa
protected override void OnElementChanged(ElementChangedEventArgs<CameraStream> elem)
{
base.OnElementChanged(elem);
if (Control == null)
{
uiCameraPreview = new UICameraPreview(elem.NewElement.Camera);
SetNativeControl(uiCameraPreview);
var camera_stream = (CameraStream)elem.NewElement;
camera_stream.flash_event += (object sender, EventArgs e) => {
Debug.Write("teeeest");
};
}
}
//Invocato quando la view viene chiusa
protected override void Dispose(bool disposing)
{
if (disposing)
{
Control.CaptureSession.Dispose();
Control.Dispose();
}
base.Dispose(disposing);
}
public void attivaFlash()
{
Debug.Write("flash");
Debug.Write("flash");
Debug.Write("flash");
}
public void distattivaFlash()
{
throw new NotImplementedException();
}
public void scattaFoto()
{
throw new NotImplementedException();
}
public void ruotaFotocamera(BottoneSwitch btn)
{
if (!btn.isON){// mostro la post
Debug.Write("mostra fotocamera post");
}else{//mostra ant
Debug.Write("mostra fotocamera ant");
}
}
}
}
因此使用此事件:
public void gestisciFlash(BottoneSwitch btn){
if (flash_event != null)
flash_event.Invoke(btn, null);
}
我应该能够打印“ teeest”,但是flash_event始终为null,我不知道该怎么办。
我尝试了firing custom render event此处提供的解决方案,但是没有运气。
有什么建议吗?
编辑
protected override void OnElementChanged(ElementChangedEventArgs<CameraStream> e)
{
if(e.OldElement != null){
}
if(e.NewElement != null){
if (Control == null)
{
uiCameraPreview = new UICameraPreview(new Camera.CameraOptions());
SetNativeControl(uiCameraPreview);
}
var camera_stream = (CameraStream)e.NewElement;
camera_stream.flash_event += (object sender, EventArgs ea) => {
attivaFlash();
};
}
base.OnElementChanged(e);
}
按@Benl的建议重构了代码,但仍然无法正常工作,
public void gestisciFlash(BottoneSwitch btn){
if (flash_event != null)
flash_event.Invoke(btn, null);
}
仍然为空。
答案 0 :(得分:0)
编辑:
假设:已附加一些生成闪光灯事件的相机设备。
CameraStreamRenderer.cs:
a=[1,2,3]
b=a #a and b point to the same object
c=list(a) #c points to different object
if a==b:
print('#') #output:#
if a is b:
print('##') #output:##
if a==c:
print('###') #output:##
if a is c:
print('####') #no output as c and a point to different object
但是,OnElementChanged(ElementChangedEventArgs<CameraStream> e)
{
base.OnElementChanged(e);
if (Control == null)
{
// Instantiate the native control and assign it to the Control property with
// the SetNativeControl method
uiCameraPreview = new UICameraPreview(e.NewElement.Camera);
SetNativeControl(uiCameraPreview);
}
if (e.OldElement != null)
{
// Unsubscribe from event handlers and cleanup any resources
uiCameraPreview.Flash -= OnFlash;
}
if (e.NewElement != null)
{
// Configure the control and subscribe to event handlers
uiCameraPreview.Flash += OnFlash;
}
}
void OnFlash(object sender, EventArgs e)
{
Debug.WriteLine("Flash");
}
事件必须在某个地方生成,而原始问题中未显示。
假定flash
事件可能是在flash
的{{1}}中使用假定的密钥UICameraPreview
生成的。
UICameraPreview.cs:
Observer
"torchActive"
上面没有显示。