在 Xamarin.iOS 中创建自定义渲染器时,如何将默认渲染器添加到视图?

问题描述

我为 Xamarin 应用程序中的按钮创建了一个自定义渲染器。 iOS 中的所有按钮都使用此渲染器。我想要一个特定的按钮不使用自定义渲染器,而是使用认渲染器。如何将认渲染器分配给一个按钮?

这是我的 Customrenderer 的外观:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import webdriverwait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select

browser = webdriver.Chrome()

url = <url>
browser.get(url)

export1 = browser.find_element_by_xpath("//*[@id=\"react-main-content\"]/div[1]/section/div[1]/div[3]/div[2]/div/button")
time.sleep(1)
export1.click()

# This is the element it's unable to locate
webdriverwait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"/html/body/div[4]/div/div/ul/li[3]/li/button")))

# Execution never reaches this point
export2 = browser.find_element_by_xpath("/html/body/div[4]/div/div/ul/li[3]/li/button")
export2.click()

解决方法

在您的共享项目中创建自定义 Button

public class CustomButton : Button
{

}

将自定义渲染器分配给 CustomButton 类型:

[assembly: ExportRenderer(typeof(CustomButton),typeof(CustomRenderer))]
namespace Test.iOS
{
    public class CustomRenderer : ButtonRenderer
{...}}

每当您希望按钮使用默认渲染器时,请使用 <Button ,并且每当您希望它使用自定义渲染器时,请使用 <CustomButton