Xamarin.forms 取消单击搜索栏时出现对象引用错误
我为searchbar创建了一个自定义渲染器来更改其UI。自定义渲染器如下所示。问题是当我点击n取消按钮时,它给出了一个对象引用错误。有人能告诉我该怎么做吗Xamarin.forms 取消单击搜索栏时出现对象引用错误,xamarin.forms,xamarin.ios,Xamarin.forms,Xamarin.ios,我为searchbar创建了一个自定义渲染器来更改其UI。自定义渲染器如下所示。问题是当我点击n取消按钮时,它给出了一个对象引用错误。有人能告诉我该怎么做吗 [assembly: ExportRendererAttribute(typeof(TransparentSearchBar),typeof(TransparentSearchBarRenderer))] namespace AmerisureMobile.iOS { public class TransparentSearchBa
[assembly: ExportRendererAttribute(typeof(TransparentSearchBar),typeof(TransparentSearchBarRenderer))]
namespace AmerisureMobile.iOS
{
public class TransparentSearchBarRenderer : SearchBarRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
{
base.OnElementChanged(e);
UISearchBar bar = this.Control;
bar.BarTintColor = UIColor.FromRGBA(155, 155, 155, 155);
bar.SetSearchFieldBackgroundImage(null, UIControlState.Normal);
bar.AutocorrectionType = UITextAutocorrectionType.No;
bar.TintColor = UIColor.FromRGBA(0, 0, 0, 0);
bar.BackgroundColor = UIColor.FromRGBA(0, 0, 0, 0);
bar.SearchBarStyle = UISearchBarStyle.Minimal;
bar.SetBackgroundImage(new UIImage(), UIBarPosition.TopAttached, UIBarMetrics.Default);
bar.SetImageforSearchBarIcon(new UIImage(), UISearchBarIcon.Clear, UIControlState.Disabled);
UITextField txSearchField = (UITextField)Control.ValueForKey(new Foundation.NSString("searchField"));
txSearchField.BackgroundColor = UIColor.White;
txSearchField.BorderStyle = UITextBorderStyle.None;
txSearchField.Layer.BorderWidth = 1.0f;
txSearchField.Layer.CornerRadius = 2.0f;
txSearchField.Layer.BorderColor = UIColor.LightGray.CGColor;
}
}
}
[程序集:ExportRenderAttribute(typeof(TransparentSearchBar)、typeof(TransparentSearchDerrer))]
名称空间AmerisureMobile.iOS
{
公共类透明搜索器:搜索器
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
UISearchBar=this.Control;
bar.BarTintColor=UIColor.FromRGBA(155155155155155);
bar.SetSearchFieldBackgroundImage(空,uicontrol状态.正常);
bar.AutocorrectionType=UITextAutocorrectionType.No;
bar.TintColor=UIColor.FromRGBA(0,0,0,0);
bar.BackgroundColor=UIColor.FromRGBA(0,0,0,0);
bar.SearchBarStyle=UISearchBarStyle.Minimal;
SetBackgroundImage(新UIImage(),UIBarPosition.TopAttached,UIBarMetrics.Default);
bar.SetImageforSearchBarIcon(新UIImage(),UISearchBarIcon.Clear,UIControlState.Disabled);
UITExtfield TXScript字段=(UITExtField)控件.ValueFrKEY(新基金会NSString(SkCHfield));
txSearchField.BackgroundColor=UIColor.White;
txSearchField.BorderStyle=UITextBorderStyle.None;
txSearchField.Layer.BorderWidth=1.0f;
txSearchField.Layer.CornerRadius=2.0f;
txSearchField.Layer.BorderColor=UIColor.LightGray.CGColor;
}
}
}
通过将委托添加到搜索栏
,尝试用click事件覆盖该方法
//***
bar.Delegate = new MySearchBarDelegate();
class MySearchBarDelegate: UISearchBarDelegate
{
public override void CancelButtonClicked(UISearchBar searchBar)
{
//debug here
}
}
您需要使用调试器来确定哪个元素为空,将调试器添加到哪里?在哪种情况下?大概有一个元素发生了变化。这就是你发布的代码在页面加载时被调用,到那时,屏幕上没有“取消”按钮,当我在框中键入内容时就会出现。那你为什么还要发布这些代码呢?如果崩溃,使用堆栈跟踪找出崩溃发生的位置,然后在那里开始调试。