在Windows窗体节点下添加部分表单类

时间:2016-06-20 13:01:35

标签: c# winforms

我目前面临的问题是我的主Form.cs文件变得越来越大,我想把它分成几个部分文件。这些应该位于 Visual Studio解决方案资源管理器中的主窗体(MainForm.cs)节点下方。

我尝试创建新的类文件并将其声明为我MainForm类的部分源文件。但是,新创建的分部类文件会更改它的图标,看起来像一个表单。相反,它应该在解决方案资源管理器中坚持 MainForm节点。

想象问题:

Wrongly ordered partial class file

MainForm.ContextActions.cs文件内容如下:

namespace Interface_Group_Editor
{
    public partial class MainForm
    {
        [...]
    }
}

内容类似于MainForm.Designer.cs文件。但是,.Designer.cs文件位于MainForm.cs节点下方,而.ContextActions.cs则没有!

我只处理MainForm.cs文件中绝对必要的表单事件,并将我的业务逻辑放在我从表单访问的外部类中。但是,单独使用我的TreeView代码来维护和编辑表单大约需要500行代码,这些代码可以重新定位到新的部分源文件中。这将有助于项目结构的整体可见性!

有没有办法强制这种行为到 Solution Explorer

1 个答案:

答案 0 :(得分:6)

IDE不允许您这样做,您必须使用文本编辑器编辑.csproj文件。记事本会做。定位:

initialized

编辑为:

# Provide R code to build the object.
shinyServer(function(input, output, session) {


# A reactive expression for filtering dataframe
Update_df1 <- reactive({
  Flux_Data_df %>%
    filter(
      Gap_filled >= input$Gap_filled[1] & 
      Gap_filled <= input$Gap_filled[2] &
      Uncert > input$Uncert[1] & 
      Uncert < input$Uncert[2] &
      Stand_Age >= input$Stand_Age[1] & 
      Stand_Age <= input$Stand_Age[2] &
      GPP > input$GPP[1] & 
      GPP < input$GPP[2] &
      MAT > input$MAT[1] & 
      MAT < input$MAT[2] &
      MAP > input$MAP[1] & 
      MAP < input$MAP[2]) %>%
    filter(
      Management %in% input$Management &
      Disturbance %in% input$Disturbance &
      Climate %in% input$Climate &
      Ecosystem %in% input$Ecosystem) %>% as.data.frame()
})

# A reactive expression to add model predicion to a new dataframe
Age<-reactive({
  prediction<- Update_df1()
  for(id in unique(prediction$Site_ID)){
    lm_Age<- try(nlsLM(NEP~offset + A*(1-exp(k*Stand_Age)), data = prediction[prediction$Site_ID != id,], 
                       start = list(A=  711.5423, k= -0.2987, offset= -444.2672),
                       lower= c(A = -Inf, k =  -Inf, offset= -1500), control = list(maxiter = 500), weights = 1/Uncert), silent=TRUE)
    prediction$f_Age[prediction$Site_ID == id] <- predict(object = lm_Age, newdata =  prediction[prediction$Site_ID == id,]) 
  }
  return(prediction)
})

Final_df<-reactive({
  df<- Age()
  for(id in unique(df$Site_ID)){
    lm_NEP<- lm(NEP~ (f_Age + Stand_Age + GPP)^2 +
                  Clay_1km + GPP:MAP + SPI_CRU_Mean:NHx + Stand_Age:NHx,
                data = df[df$Site_ID != id,], weights = 1/Uncert)
    df$prediction[df$Site_ID == id] <- predict(object = lm_NEP, newdata = df[df$Site_ID == id,]) 
  }
  return(df)
})

Model_Performance<- reactive({
  Stat<- data.frame(matrix(ncol = 3, nrow = 1))
  colnames(Stat)<- c("R2", "MEF", "RMSE")
  Stat$R2<- round(cor(Final_df()$prediction, Final_df()$NEP, use="complete")^2, digits = 2)
  Stat$RMSE <- round(rmse(Final_df()$prediction, Final_df()$NEP), digits = 2)
  Stat$MEF<-round(NSE(Final_df()$prediction, Final_df()$NEP, na.rm=TRUE), digits=2)
  return(Stat)
})

Var_Imp<- reactive({
  Imp<- data.frame(matrix(ncol = 7, nrow = 1))
  colnames(Imp)<- c("Age", "GPP*Age", "GPP*MAP", "Clay content", "Ndepo*SPI", "GPP", "Ndepo*Age")
  VarImp_NEP<- varImp(lm(NEP ~ (f_Age + Stand_Age + GPP)^2 +
                           Clay_1km + GPP:MAP + SPI_CRU_Mean:NHx + Stand_Age:NHx,
                         data=Final_df(), weights = 1/Uncert))
  Imp$Age<- (VarImp_NEP$Overall[1] + VarImp_NEP$Overall[2] + VarImp_NEP$Overall[5])/ sum(VarImp_NEP$Overall)
  Imp["GPP*Age"]<- (VarImp_NEP$Overall[6] + VarImp_NEP$Overall[7])/ sum(VarImp_NEP$Overall) 
  Imp["GPP*MAP"]<- VarImp_NEP$Overall[8]/ sum(VarImp_NEP$Overall) 
  Imp["Clay content"]<- VarImp_NEP$Overall[4]/ sum(VarImp_NEP$Overall) 
  Imp["Ndepo*SPI"]<- VarImp_NEP$Overall[9]/ sum(VarImp_NEP$Overall)
  Imp["GPP"]<- VarImp_NEP$Overall[3]/ sum(VarImp_NEP$Overall)
  Imp["Ndepo*Age"]<- VarImp_NEP$Overall[10]/ sum(VarImp_NEP$Overall)
  Imp<- gather(Imp)
  colnames(Imp)<- c("Variable", "Percentage")
  Imp$Percentage<- round(Imp$Percentage*100, digits = 1)
  return(Imp)
})

#Plot Univariate
output$Univariate <-  renderRbokeh({
  plot_data<- Final_df()
  plot_data$Stand_Age<- round(plot_data$Stand_Age, digits = 0)
  plot_data$Stand_Age<- round(plot_data$Stand_Age, digits = 0)
g<- figure() %>%
    ly_points(x = input$xvar, y = input$yvar, data=plot_data, hover= c(Site_ID, Stand_Age)) %>%
  x_axis("x", label = names(axis_vars)[axis_vars == input$xvar]) %>%
  y_axis("y", label = names(axis_vars)[axis_vars == input$yvar])
return(g)
})

#Plot model performance
output$Model_perf <-  renderRbokeh({
  plot_data<- Final_df()
  plot_data$Stand_Age<- round(plot_data$Stand_Age, digits = 0)
  g<- figure() %>%
    ly_points(x = prediction, y = NEP, data=plot_data, hover= c(Site_ID, Stand_Age, Ecosystem)) %>%
    ly_abline(a=0, b=1) %>%
    x_axis("NEP predicted [gC.m-2.y-1]") %>%
    y_axis("NEP observed [gC.m-2.y-1]") %>%
    x_range(c(-700, 1500)) %>%
    y_range(c(-700, 1500))
  return(g)
})

#Plot Variable importance
output$Var_Imp <-  renderRbokeh({
  plot_data<- Var_Imp()
  g<- figure() %>%
    ly_points(x =Percentage, y = Variable, data=plot_data, hover= c(Percentage)) %>%
    x_axis("Percentage [%]") %>%
    y_axis("")
  return(g)
})

output$Map_Site <- renderRbokeh({
  plot_data<- Final_df()
  plot_data$Stand_Age<- round(plot_data$Stand_Age, digits = 0)
  p<- gmap(lat=0, lng=0, zoom = 2, width = 600, height = 1000, map_type ="hybrid") %>%
  ly_points(x=Long, y=Lat, data = plot_data, hover= c(Site_ID, Stand_Age), col = "red", size=5) %>%
    tool_box_select() %>%
    tool_lasso_select() %>%
    tool_reset() %>%
    tool_resize()
  return(p)
})

output$Update_data = renderDataTable({
  Final_df()
})

output$Summary_Table = renderDataTable({
  Model_Performance()

})

output$downloadData <- downloadHandler(
  filename = function() {paste('Updated.csv', sep='') },
  content = function(file) {
    write.csv(Final_df(), file)
  }
)

})

请注意MainForm.Designer.cs的条目如何显示。请记住,此功能不经常进行测试,因此您可能会遇到一些怪癖。据我所知,它可以很好地运行,例如,当您移动它们时,不会让事件处理程序返回。删除事件处理程序是一个棘手的场景,我注意到MainForm.cs文件被标记为已修改,即使它从未更改过。并且它不会像通常那样删除空事件处理程序。 YMMV。

继承是Winforms中的终极强大工具。例如,您可以从TreeView轻松派生自己的类,并将所有自定义代码移动到该类中。它不会多次使用。但也许有一天你会:)