C#UWP应用XAML(身体质量指数计算器 - BMI)
我需要TextBox txtKG.Text
并将其传递给txtDescription.Text
我假设我必须以某种方式创建TextBox类的实例,但我坚持这个错误2天。我尝试了很多方法,但我不知道该怎么做。
错误消息:
System.NullReferenceException:“对象引用未设置为对象的实例。”
MY XAML细分:
<Slider x:Name="sldKG" HorizontalAlignment="Left" Margin="170,306,0,0" VerticalAlignment="Top" Width="184" ValueChanged="Slider_ValueChanged" Minimum="40" Maximum="150" Value="70"/>
<TextBox x:Name="txtKG" FontSize="18" HorizontalAlignment="Left" Margin="50,310,0,0" VerticalAlignment="Top" TextAlignment="Center" Text="{Binding ElementName=sldKG,Path=Value}"/>
<TextBlock FontSize="18" x:Name="kg" HorizontalAlignment="Left" Margin="121,316,0,0" Text="kg" TextWrapping="Wrap" VerticalAlignment="Top"/>
<Slider x:Name="sldCM" HorizontalAlignment="Left" Margin="170,366,0,0" VerticalAlignment="Top" Width="184" ValueChanged="Slider_ValueChanged" Minimum="145" Maximum="240" Value="170"/>
<TextBlock FontSize="18" x:Name="txtDescription" HorizontalAlignment="Left" Margin="52,499,0,0" Text="Type Your parameters and i wil show Your BMI" TextWrapping="Wrap" VerticalAlignment="Top" Height="96" Width="302" TextAlignment="Center"/>
MyPage.xaml.cs
namespace Kalkulator_BMI_UWP
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void Slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
txtDescription.Text = txtKG.Text; // <!-- HERE IS THIS ERROR
}
答案 0 :(得分:0)
问题在于Slider值更改事件,因为尚未加载控件(XAML中的TextBlocks)而发生错误, 作为一种解决方法(我在一些项目中执行),您可以使用try-catch语句封装该方法,如下所示:
library('shiny')
ui<-fluidPage(
titlePanel("Sample Size Calculation"),
numericInput("num1",
h3("Power to be reached"),
value = 0.85, min=0, max=1,step=0.01),
numericInput("num2",
h3("Scale parameter"),value=25,step=0.1),
numericInput("num3",
h3("delta"),
value = 2,step=0.01),
numericInput("num4",
h3("allocation ratio"),
value = 1,min=0, step=0.01),
numericInput("num5",
h3("Beta"),
value = 0.1062, min=0, step=0.01),
numericInput("num6",
h3("event probability"),
value = 1,min=0, max=1,step=0.01),
numericInput("num7",
h3("Significant level"),
value = 0.05, min=0, max=1,step=0.01),
plotOutput("picture"),
textOutput("k_gamma"),
submitButton("Submit")
)
server<-function(input,output){
n=2
power1=rep(NA,2*n)
ndf=rep(NA,2*n)
ddf=rep(NA,2*n)
f_crit1=rep(NA,2*n)
f_crit2=rep(NA,2*n)
criticalvalue1=rep(NA,2*n)
criticalvalue2=rep(NA,2*n)
x=rep(NA,2*n)
my_func<-reactive({
k_gamma<-input$num2
delta_pt<-input$num3
r<-input$num4
beta<-input$num5
eventprob<-input$num4
alpha<-input$num7
for (i in 1:(2*n)) {
ndf[i]=2*ceiling(eventprob*i)*k_gamma
ddf[i]=2*ceiling(eventprob*i)*r*k_gamma
f_crit1[i]= qf(1-alpha,ndf[i],ddf[i])
f_crit2[i] = qf(alpha,ndf[i],ddf[i])
criticalvalue1[i]=(f_crit1[i]*r)/((delta_pt**(beta)))
criticalvalue2[i]=(f_crit2[i]*r)/((delta_pt**(beta)))
power1[i]=1-pf(criticalvalue1[i],ndf[i],ddf[i])+pf(criticalvalue2[i],ndf[i],ddf[i])
samplesize1=c(ceiling(n/2):(2*n))
powervector1=power1[ceiling(n/2):(2*n)]
}
})
output$picture<-renderPlot({plot(my_func()$samplesize1, my_func()$powervector1 ,xlim=c(0,200),ylim=c(0,1))})
}
shinyApp(ui, server)